Skip to content

Commit e9c3950

Browse files
TomAugspurgerPingviinituutti
authored andcommitted
DOC: Document AttributeError for accessor (pandas-dev#24855)
Closes pandas-dev#20579
1 parent f47d197 commit e9c3950

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/source/extending.rst

+13
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ decorate a class, providing the name of attribute to add. The class's
2828
@pd.api.extensions.register_dataframe_accessor("geo")
2929
class GeoAccessor(object):
3030
def __init__(self, pandas_obj):
31+
self._validate(pandas_obj)
3132
self._obj = pandas_obj
3233
34+
@staticmethod
35+
def _validate(obj):
36+
if 'lat' not in obj.columns or 'lon' not in obj.columns:
37+
raise AttributeError("Must have 'lat' and 'lon'.")
38+
3339
@property
3440
def center(self):
3541
# return the geographic center point of this DataFrame
@@ -54,6 +60,13 @@ This can be a convenient way to extend pandas objects without subclassing them.
5460
If you write a custom accessor, make a pull request adding it to our
5561
:ref:`ecosystem` page.
5662

63+
We highly recommend validating the data in your accessor's `__init__`.
64+
In our ``GeoAccessor``, we validate that the data contains the expected columns,
65+
raising an ``AttributeError`` when the validation fails.
66+
For a ``Series`` accessor, you should validate the ``dtype`` if the accessor
67+
applies only to certain dtypes.
68+
69+
5770
.. _extending.extension-types:
5871

5972
Extension Types

0 commit comments

Comments
 (0)