File tree 1 file changed +13
-0
lines changed
1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -28,8 +28,14 @@ decorate a class, providing the name of attribute to add. The class's
28
28
@pd.api.extensions.register_dataframe_accessor (" geo" )
29
29
class GeoAccessor (object ):
30
30
def __init__ (self , pandas_obj ):
31
+ self ._validate(pandas_obj)
31
32
self ._obj = pandas_obj
32
33
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
+
33
39
@ property
34
40
def center (self ):
35
41
# 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.
54
60
If you write a custom accessor, make a pull request adding it to our
55
61
:ref: `ecosystem ` page.
56
62
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
+
57
70
.. _extending.extension-types :
58
71
59
72
Extension Types
You can’t perform that action at this time.
0 commit comments