Skip to content

Provide ExtensionDtype.construct_from_string by default #26562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,6 @@ def construct_array_type(cls):
"""
return IntegerArray

@classmethod
def construct_from_string(cls, string):
"""
Construction from a string, raise a TypeError if not
possible
"""
if string == cls.name:
return cls()
raise TypeError("Cannot construct a '{}' from "
"'{}'".format(cls, string))


def integer_array(values, dtype=None, copy=False):
"""
Expand Down
34 changes: 25 additions & 9 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,26 @@ def construct_array_type(cls):
@classmethod
def construct_from_string(cls, string):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add types

"""
Attempt to construct this type from a string.
Construct this type from a string.

This is useful mainly for data types that accept parameters.
For example, datetime types can accept units (e.g. nanoseconds)
or timezones. In those cases, the expected type could be
something like ``datetime64[ns, UTC]``.

By default, in the abstract class, just the name of the type is
expected. But subclasses can overwrite this method to accept
type parameters.

Parameters
----------
string : str
The name of the type, for example ``category``.

Returns
-------
self : instance of 'cls'
ExtensionDtype
Instance of the dtype.

Raises
------
Expand All @@ -191,18 +202,23 @@ def construct_from_string(cls, string):

Examples
--------
If the extension dtype can be constructed without any arguments,
the following may be an adequate implementation.
For extension dtypes with arguments the following may be an
adequate implementation.

>>> @classmethod
... def construct_from_string(cls, string)
... if string == cls.name:
... return cls()
... def construct_from_string(cls, string):
... pattern = re.compile(r"^my_type\[(?P<arg_name>.+)\]$") # noqa
... match = pattern.match(string)
... if match:
... return cls(**match.groupdict())
... else:
... raise TypeError("Cannot construct a '{}' from "
... "'{}'".format(cls, string))
... "'{}'".format(cls.__name__, string))
"""
raise AbstractMethodError(cls)
if string != cls.name:
raise TypeError("Cannot construct a '{}' from '{}'".format(
cls.__name__, string))
return cls()

@classmethod
def is_dtype(cls, dtype):
Expand Down