Skip to content

Commit 1c5ac85

Browse files
Added doc at extending.rst
- Improved accessor.py docstr Co-authored-by: Afonso Antunes <[email protected]>
1 parent 0eb5bc1 commit 1c5ac85

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

doc/source/development/extending.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,62 @@ For a ``Series`` accessor, you should validate the ``dtype`` if the accessor
6969
applies only to certain dtypes.
7070

7171

72+
Registering accessors via entry points
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
You can create a custom accessor for a pandas object and expose it via Python's
76+
entry point system. Once installed using pip, the accessor can be automatically
77+
discovered and registered by pandas at runtime, without requiring manual import.
78+
79+
To register the entry point for your accessor, follow the format shown below:
80+
81+
.. code-block:: python
82+
83+
# setup.py
84+
entry_points={
85+
'pandas.DataFrame.accessor': [ '<name> = <module>:<AccessorClass>', ... ],
86+
'pandas.Series.accessor': [ '<name> = <module>:<AccessorClass>', ... ],
87+
'pandas.Index.accessor': [ '<name> = <module>:<AccessorClass>', ... ],
88+
}
89+
90+
Alternatively, if you are using a ``pyproject.toml``-based build:
91+
92+
.. code-block:: toml
93+
94+
# pyproject.toml
95+
[project.entry-points."pandas.DataFrame.accessor"]
96+
<name> = "<module>:<AccessorClass>"
97+
98+
[project.entry-points."pandas.Series.accessor"]
99+
<name> = "<module>:<AccessorClass>"
100+
101+
[project.entry-points."pandas.Index.accessor"]
102+
<name> = "<module>:<AccessorClass>"
103+
104+
105+
Assuming the accessor class ``GeoAccessor`` is defined in the module
106+
``geoPlugin.geo_accessor``, and using the accessor name ``geo`` as in the
107+
example above:
108+
109+
.. code-block:: python
110+
111+
# setup.py
112+
entry_points={
113+
'pandas.DataFrame.accessor': [ 'geo = geoPlugin.geo_accessor:GeoAccessor' ],
114+
}
115+
116+
Or, for a ``pyproject.toml``-based build:
117+
118+
.. code-block:: toml
119+
120+
# pyproject.toml
121+
[project.entry-points."pandas.DataFrame.accessor"]
122+
geo = "geoPlugin.geo_accessor:GeoAccessor"
123+
124+
125+
For background on Python's Entry Point system and Plugins:
126+
https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#plugin-entry-points
127+
72128
.. _extending.extension-types:
73129

74130
Extension types

pandas/core/accessor.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,10 @@ def accessor_entry_point_loader() -> None:
405405
Load and register pandas accessors declared via entry points.
406406
407407
This function scans the 'pandas.<pd_obj>.accessor' entry point group for
408-
accessors registered by third-party packages. Each entry point is expected
409-
to follow the format:
408+
accessors registered by third-party packages. These accessors extend
409+
core pandas objects (`DataFrame`, `Series`, `Index`).
410+
411+
Each entry point is expected to follow the format:
410412
411413
# setup.py
412414
entry_points={
@@ -415,7 +417,7 @@ def accessor_entry_point_loader() -> None:
415417
'pandas.Index.accessor': [ <name> = <module>:<AccessorClass>, ... ],
416418
}
417419
418-
OR for pyproject.toml file:
420+
OR using pyproject.toml file:
419421
420422
# pyproject.toml
421423
[project.entry-points."pandas.DataFrame.accessor"]
@@ -427,9 +429,6 @@ def accessor_entry_point_loader() -> None:
427429
[project.entry-points."pandas.Index.accessor"]
428430
<name> = "<module>:<AccessorClass>"
429431
430-
For more information about entrypoints:
431-
https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#plugin-entry-points
432-
433432
434433
For each valid entry point:
435434
- The accessor class is dynamically imported and registered using
@@ -441,7 +440,16 @@ def accessor_entry_point_loader() -> None:
441440
Notes
442441
-----
443442
- This function is only intended to be called at pandas startup.
444-
- For more information about accessors read their documentation.
443+
- For more information about accessors, refer to:
444+
- Pandas documentation on extending accessors:
445+
https://pandas.pydata.org/docs/development/extending.html#registering-custom-accessors
446+
- Series accessor API reference:
447+
https://pandas.pydata.org/docs/reference/series.html#accessors
448+
- Note: DataFrame and Index accessors (e.g., `.sparse`, `.str`) use the same
449+
mechanism but are not listed in separate reference pages as of now.
450+
451+
- For background on Python plugin entry points:
452+
https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#plugin-entry-points
445453
446454
Raises
447455
------

0 commit comments

Comments
 (0)