pandas.api.extensions.ExtensionArray — pandas 3.0.0.dev0+2095.g2e141aaf99 documentation (original) (raw)

class pandas.api.extensions.ExtensionArray[source]#

Abstract base class for custom 1-D array types.

pandas will recognize instances of this class as proper arrays with a custom type and will not attempt to coerce them to objects. They may be stored directly inside a DataFrame or Series.

Attributes

Methods

See also

api.extensions.ExtensionDtype

A custom data type, to be paired with an ExtensionArray.

api.extensions.ExtensionArray.dtype

An instance of ExtensionDtype.

Notes

The interface includes the following abstract methods that must be implemented by subclasses:

A default repr displaying the type, (truncated) data, length, and dtype is provided. It can be customized or replaced by by overriding:

Some methods require casting the ExtensionArray to an ndarray of Python objects with self.astype(object), which may be expensive. When performance is a concern, we highly recommend overriding the following methods:

The remaining methods implemented on this class should be performant, as they only compose abstract methods. Still, a more efficient implementation may be available, and these methods can be overridden.

One can implement methods to handle array accumulations or reductions.

One can implement methods to handle parsing from strings that will be used in methods such as pandas.io.parsers.read_csv.

This class does not inherit from ‘abc.ABCMeta’ for performance reasons. Methods and properties required by the interface raisepandas.errors.AbstractMethodError and no register method is provided for registering virtual subclasses.

ExtensionArrays are limited to 1 dimension.

They may be backed by none, one, or many NumPy arrays. For example,pandas.Categorical is an extension array backed by two arrays, one for codes and one for categories. An array of IPv6 address may be backed by a NumPy structured array with two fields, one for the lower 64 bits and one for the upper 64 bits. Or they may be backed by some other storage type, like Python lists. Pandas makes no assumptions on how the data are stored, just that it can be converted to a NumPy array. The ExtensionArray interface does not impose any rules on how this data is stored. However, currently, the backing data cannot be stored in attributes called .values or ._values to ensure full compatibility with pandas internals. But other names as .data, ._data,._items, … can be freely used.

If implementing NumPy’s __array_ufunc__ interface, pandas expects that

  1. You defer by returning NotImplemented when any Series are present in inputs. Pandas will extract the arrays and call the ufunc again.
  2. You define a _HANDLED_TYPES tuple as an attribute on the class. Pandas inspect this to determine whether the ufunc is valid for the types present.

See NumPy universal functions for more.

By default, ExtensionArrays are not hashable. Immutable subclasses may override this behavior.

Examples

Please see the following:

pandas-dev/pandas