classmethod ExtensionDtype.construct_from_string(string: str) [source]
Construct this type from a string.
This is useful mainly for data types that accept parameters. For example, a period dtype accepts a frequency parameter that can be set as period[H] (where H means hourly frequency).
By default, in the abstract class, just the name of the type is expected. But subclasses can overwrite this method to accept parameters.
| Parameters: |
|
|---|---|
| Returns: |
|
| Raises: |
|
For extension dtypes with arguments the following may be an adequate implementation.
>>> @classmethod
... def construct_from_string(cls, string):
... pattern = re.compile(r"^my_type\[(?P<arg_name>.+)\]$")
... match = pattern.match(string)
... if match:
... return cls(**match.groupdict())
... else:
... raise TypeError("Cannot construct a '{}' from "
... "'{}'".format(cls.__name__, string))
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.api.extensions.ExtensionDtype.construct_from_string.html