pandas.api.extensions.Accessor#

class pandas.api.extensions.Accessor(name, accessor)[source]#

Descriptor that exposes an accessor class as an attribute namespace.

The register_*_accessor decorators use this to attach accessors. Use it directly to add or override an accessor on a single class, such as a subclass of DataFrame, without changing any other class.

Parameters:
namestr

Name of the accessor. Should match the attribute name it is assigned to.

accessortype

Class with the extension methods. Its __init__ receives the Series, DataFrame or Index the attribute is accessed on. A new instance is created on every attribute access.

See also

register_dataframe_accessor

Register a custom accessor on DataFrame objects.

register_series_accessor

Register a custom accessor on Series objects.

register_index_accessor

Register a custom accessor on Index objects.

Examples

Override plot on a DataFrame subclass only:

>>> class MyPlotAccessor:
...     def __init__(self, pandas_obj):
...         self._obj = pandas_obj
...
...     def __call__(self):
...         return f"plotting {len(self._obj)} rows"
>>> class MyFrame(pd.DataFrame):
...     plot = pd.api.extensions.Accessor("plot", MyPlotAccessor)
>>> MyFrame({"a": [1, 2]}).plot()
'plotting 2 rows'