pandas.api.extensions.ExtensionDtype#
- class pandas.api.extensions.ExtensionDtype[source]#
A custom data type, to be paired with an ExtensionArray.
See also
extensions.register_extension_dtypeRegister an ExtensionType with pandas as class decorator.
extensions.ExtensionArrayAbstract base class for custom 1-D array types.
Notes
The interface includes the following abstract methods that must be implemented by subclasses:
type
name
construct_array_type
The following attributes and methods influence the behavior of the dtype in pandas operations
_is_numeric
_is_boolean
_get_common_dtype
The na_value class attribute can be used to set the default NA value for this type.
numpy.nanis used by default.ExtensionDtypes are required to be hashable. The base class provides a default implementation, which relies on the
_metadataclass attribute._metadatashould be a tuple containing the strings that define your data type. For example, withPeriodDtypethat’s thefreqattribute.If you have a parametrized dtype you should set the ``_metadata`` class property.
Ideally, the attributes in
_metadatawill match the parameters to yourExtensionDtype.__init__(if any). If any of the attributes in_metadatadon’t implement the standard__eq__or__hash__, the default implementations here will not work.Examples
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:>>> import pyarrow >>> from pandas.api.extensions import ExtensionArray >>> class ExtensionDtype: ... def __from_arrow__( ... self, ... array: 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.AbstractMethodErrorand noregistermethod is provided for registering virtual subclasses.Attributes
index_classThe Index subclass to return from Index.__new__ when this dtype is encountered.
kindA character code (one of 'biufcmMOSUV'), default 'O'
na_valueDefault NA value to use for this type.
nameA string identifying the data type.
namesOrdered list of field names, or None if there are no fields.
typeThe scalar type for the array, e.g.
Methods
construct_array_type()Return the array type associated with this dtype.
construct_from_string(string)Construct this type from a string.
empty(shape)Construct an ExtensionArray of this dtype with the given shape.
is_dtype(dtype)Check if we match 'dtype'.