pandas.api.extensions.
ExtensionDtype
A custom data type, to be paired with an ExtensionArray.
New in version 0.23.0.
See also
extensions.register_extension_dtype
extensions.ExtensionArray
Notes
The interface includes the following abstract methods that must be implemented by subclasses:
type
name
The following attributes and methods influence the behavior of the dtype in pandas operations
_is_numeric
_is_boolean
_get_common_dtype
Optionally one can override construct_array_type for construction with the name of this dtype via the Registry. See extensions.register_extension_dtype().
extensions.register_extension_dtype()
construct_array_type
The na_value class attribute can be used to set the default NA value for this type. numpy.nan is used by default.
numpy.nan
ExtensionDtypes are required to be hashable. The base class provides a default implementation, which relies on the _metadata class attribute. _metadata should be a tuple containing the strings that define your data type. For example, with PeriodDtype that’s the freq attribute.
_metadata
PeriodDtype
freq
If you have a parametrized dtype you should set the ``_metadata`` class property.
Ideally, the attributes in _metadata will match the parameters to your ExtensionDtype.__init__ (if any). If any of the attributes in _metadata don’t implement the standard __eq__ or __hash__, the default implementations here will not work.
ExtensionDtype.__init__
__eq__
__hash__
Changed in version 0.24.0: Added _metadata, __hash__, and changed the default definition of __eq__.
For interaction with Apache Arrow (pyarrow), a __from_arrow__ method can be implemented: this method receives a pyarrow Array or ChunkedArray as only argument and is expected to return the appropriate pandas ExtensionArray for this dtype and the passed values:
__from_arrow__
class ExtensionDtype: def __from_arrow__( self, array: Union[pyarrow.Array, pyarrow.ChunkedArray] ) -> ExtensionArray: ...
This class does not inherit from ‘abc.ABCMeta’ for performance reasons. Methods and properties required by the interface raise pandas.errors.AbstractMethodError and no register method is provided for registering virtual subclasses.
pandas.errors.AbstractMethodError
register
Attributes
kind
A character code (one of ‘biufcmMOSUV’), default ‘O’
na_value
Default NA value to use for this type.
A string identifying the data type.
names
Ordered list of field names, or None if there are no fields.
The scalar type for the array, e.g.
Methods
construct_array_type()
Return the array type associated with this dtype.
construct_from_string(string)
construct_from_string
Construct this type from a string.
is_dtype(dtype)
is_dtype
Check if we match ‘dtype’.