|
7 | 7 | from typing import FrozenSet, Set
|
8 | 8 | import warnings
|
9 | 9 |
|
10 |
| -from pandas.util._decorators import Appender |
| 10 | +from pandas.util._decorators import doc |
11 | 11 |
|
12 | 12 |
|
13 | 13 | class DirNamesMixin:
|
@@ -193,122 +193,119 @@ def __get__(self, obj, cls):
|
193 | 193 | return accessor_obj
|
194 | 194 |
|
195 | 195 |
|
| 196 | +@doc(klass="", others="") |
196 | 197 | def _register_accessor(name, cls):
|
197 |
| - def decorator(accessor): |
198 |
| - if hasattr(cls, name): |
199 |
| - warnings.warn( |
200 |
| - f"registration of accessor {repr(accessor)} under name " |
201 |
| - f"{repr(name)} for type {repr(cls)} is overriding a preexisting" |
202 |
| - f"attribute with the same name.", |
203 |
| - UserWarning, |
204 |
| - stacklevel=2, |
205 |
| - ) |
206 |
| - setattr(cls, name, CachedAccessor(name, accessor)) |
207 |
| - cls._accessors.add(name) |
208 |
| - return accessor |
209 |
| - |
210 |
| - return decorator |
| 198 | + """ |
| 199 | + Register a custom accessor on {klass} objects. |
211 | 200 |
|
| 201 | + Parameters |
| 202 | + ---------- |
| 203 | + name : str |
| 204 | + Name under which the accessor should be registered. A warning is issued |
| 205 | + if this name conflicts with a preexisting attribute. |
212 | 206 |
|
213 |
| -_doc = """ |
214 |
| -Register a custom accessor on %(klass)s objects. |
| 207 | + Returns |
| 208 | + ------- |
| 209 | + callable |
| 210 | + A class decorator. |
215 | 211 |
|
216 |
| -Parameters |
217 |
| ----------- |
218 |
| -name : str |
219 |
| - Name under which the accessor should be registered. A warning is issued |
220 |
| - if this name conflicts with a preexisting attribute. |
| 212 | + See Also |
| 213 | + -------- |
| 214 | + {others} |
221 | 215 |
|
222 |
| -Returns |
223 |
| -------- |
224 |
| -callable |
225 |
| - A class decorator. |
| 216 | + Notes |
| 217 | + ----- |
| 218 | + When accessed, your accessor will be initialized with the pandas object |
| 219 | + the user is interacting with. So the signature must be |
226 | 220 |
|
227 |
| -See Also |
228 |
| --------- |
229 |
| -%(others)s |
| 221 | + .. code-block:: python |
230 | 222 |
|
231 |
| -Notes |
232 |
| ------ |
233 |
| -When accessed, your accessor will be initialized with the pandas object |
234 |
| -the user is interacting with. So the signature must be |
| 223 | + def __init__(self, pandas_object): # noqa: E999 |
| 224 | + ... |
235 | 225 |
|
236 |
| -.. code-block:: python |
| 226 | + For consistency with pandas methods, you should raise an ``AttributeError`` |
| 227 | + if the data passed to your accessor has an incorrect dtype. |
237 | 228 |
|
238 |
| - def __init__(self, pandas_object): # noqa: E999 |
239 |
| - ... |
| 229 | + >>> pd.Series(['a', 'b']).dt |
| 230 | + Traceback (most recent call last): |
| 231 | + ... |
| 232 | + AttributeError: Can only use .dt accessor with datetimelike values |
240 | 233 |
|
241 |
| -For consistency with pandas methods, you should raise an ``AttributeError`` |
242 |
| -if the data passed to your accessor has an incorrect dtype. |
| 234 | + Examples |
| 235 | + -------- |
243 | 236 |
|
244 |
| ->>> pd.Series(['a', 'b']).dt |
245 |
| -Traceback (most recent call last): |
246 |
| -... |
247 |
| -AttributeError: Can only use .dt accessor with datetimelike values |
| 237 | + In your library code:: |
248 | 238 |
|
249 |
| -Examples |
250 |
| --------- |
| 239 | + import pandas as pd |
251 | 240 |
|
252 |
| -In your library code:: |
| 241 | + @pd.api.extensions.register_dataframe_accessor("geo") |
| 242 | + class GeoAccessor: |
| 243 | + def __init__(self, pandas_obj): |
| 244 | + self._obj = pandas_obj |
253 | 245 |
|
254 |
| - import pandas as pd |
| 246 | + @property |
| 247 | + def center(self): |
| 248 | + # return the geographic center point of this DataFrame |
| 249 | + lat = self._obj.latitude |
| 250 | + lon = self._obj.longitude |
| 251 | + return (float(lon.mean()), float(lat.mean())) |
255 | 252 |
|
256 |
| - @pd.api.extensions.register_dataframe_accessor("geo") |
257 |
| - class GeoAccessor: |
258 |
| - def __init__(self, pandas_obj): |
259 |
| - self._obj = pandas_obj |
| 253 | + def plot(self): |
| 254 | + # plot this array's data on a map, e.g., using Cartopy |
| 255 | + pass |
260 | 256 |
|
261 |
| - @property |
262 |
| - def center(self): |
263 |
| - # return the geographic center point of this DataFrame |
264 |
| - lat = self._obj.latitude |
265 |
| - lon = self._obj.longitude |
266 |
| - return (float(lon.mean()), float(lat.mean())) |
| 257 | + Back in an interactive IPython session: |
267 | 258 |
|
268 |
| - def plot(self): |
269 |
| - # plot this array's data on a map, e.g., using Cartopy |
270 |
| - pass |
| 259 | + >>> ds = pd.DataFrame({{'longitude': np.linspace(0, 10), |
| 260 | + ... 'latitude': np.linspace(0, 20)}}) |
| 261 | + >>> ds.geo.center |
| 262 | + (5.0, 10.0) |
| 263 | + >>> ds.geo.plot() |
| 264 | + # plots data on a map |
| 265 | + """ |
271 | 266 |
|
272 |
| -Back in an interactive IPython session: |
| 267 | + def decorator(accessor): |
| 268 | + if hasattr(cls, name): |
| 269 | + warnings.warn( |
| 270 | + f"registration of accessor {repr(accessor)} under name " |
| 271 | + f"{repr(name)} for type {repr(cls)} is overriding a preexisting" |
| 272 | + f"attribute with the same name.", |
| 273 | + UserWarning, |
| 274 | + stacklevel=2, |
| 275 | + ) |
| 276 | + setattr(cls, name, CachedAccessor(name, accessor)) |
| 277 | + cls._accessors.add(name) |
| 278 | + return accessor |
273 | 279 |
|
274 |
| - >>> ds = pd.DataFrame({'longitude': np.linspace(0, 10), |
275 |
| - ... 'latitude': np.linspace(0, 20)}) |
276 |
| - >>> ds.geo.center |
277 |
| - (5.0, 10.0) |
278 |
| - >>> ds.geo.plot() |
279 |
| - # plots data on a map |
280 |
| -""" |
| 280 | + return decorator |
281 | 281 |
|
282 | 282 |
|
283 |
| -@Appender( |
284 |
| - _doc |
285 |
| - % dict( |
286 |
| - klass="DataFrame", others=("register_series_accessor, register_index_accessor") |
287 |
| - ) |
| 283 | +@doc( |
| 284 | + _register_accessor, |
| 285 | + klass="DataFrame", |
| 286 | + others="register_series_accessor, register_index_accessor", |
288 | 287 | )
|
289 | 288 | def register_dataframe_accessor(name):
|
290 | 289 | from pandas import DataFrame
|
291 | 290 |
|
292 | 291 | return _register_accessor(name, DataFrame)
|
293 | 292 |
|
294 | 293 |
|
295 |
| -@Appender( |
296 |
| - _doc |
297 |
| - % dict( |
298 |
| - klass="Series", others=("register_dataframe_accessor, register_index_accessor") |
299 |
| - ) |
| 294 | +@doc( |
| 295 | + _register_accessor, |
| 296 | + klass="Series", |
| 297 | + others="register_dataframe_accessor, register_index_accessor", |
300 | 298 | )
|
301 | 299 | def register_series_accessor(name):
|
302 | 300 | from pandas import Series
|
303 | 301 |
|
304 | 302 | return _register_accessor(name, Series)
|
305 | 303 |
|
306 | 304 |
|
307 |
| -@Appender( |
308 |
| - _doc |
309 |
| - % dict( |
310 |
| - klass="Index", others=("register_dataframe_accessor, register_series_accessor") |
311 |
| - ) |
| 305 | +@doc( |
| 306 | + _register_accessor, |
| 307 | + klass="Index", |
| 308 | + others="register_dataframe_accessor, register_series_accessor", |
312 | 309 | )
|
313 | 310 | def register_index_accessor(name):
|
314 | 311 | from pandas import Index
|
|
0 commit comments