pandas.api.extensions.Accessor#
- class pandas.api.extensions.Accessor(name, accessor)[source]#
Descriptor that exposes an accessor class as an attribute namespace.
The
register_*_accessordecorators use this to attach accessors. Use it directly to add or override an accessor on a single class, such as a subclass ofDataFrame, 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 theSeries,DataFrameorIndexthe attribute is accessed on. A new instance is created on every attribute access.
See also
register_dataframe_accessorRegister a custom accessor on DataFrame objects.
register_series_accessorRegister a custom accessor on Series objects.
register_index_accessorRegister a custom accessor on Index objects.
Examples
Override
ploton 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'