1
1
"""Register functions as methods of Pandas DataFrame and Series."""
2
+
2
3
from __future__ import annotations
3
4
4
5
import warnings
@@ -323,7 +324,7 @@ def register_groupby_accessor(name: str):
323
324
return _register_accessor (name , DataFrameGroupBy )
324
325
325
326
326
- def register_groupby_method (method : Callable ) -> Callable :
327
+ def register_groupby_method (method : Callable ):
327
328
"""Register a function as a method attached to the pandas DataFrameGroupBy.
328
329
329
330
Example:
@@ -335,22 +336,60 @@ def register_groupby_method(method: Callable) -> Callable:
335
336
!!! info "New in version 0.7.0"
336
337
337
338
Args:
338
- method: Function to be registered as a method on the DataFrame.
339
+ method: Function to be registered as a method
340
+ on the DataFrameGroupBy object.
339
341
340
342
Returns:
341
- A Callable .
343
+ callable: The original method .
342
344
"""
345
+ method_signature = inspect .signature (method )
343
346
344
347
def inner (* args : tuple , ** kwargs : dict ):
348
+ """Inner function to register the method.
349
+
350
+ This function is called when the user
351
+ decorates a function with register_groupby_method.
352
+
353
+ Args:
354
+ *args: The arguments to pass to the registered method.
355
+ **kwargs: The keyword arguments to pass to the registered method.
356
+
357
+ Returns:
358
+ method: The original method.
359
+ """
360
+
345
361
class AccessorMethod (object ):
362
+ """DataFrameGroupBy Accessor method class."""
363
+
346
364
__doc__ = method .__doc__
347
365
348
366
def __init__ (self , obj ):
367
+ """Initialize the accessor method class.
368
+
369
+ Args:
370
+ obj: The pandas DataFrameGroupBy object.
371
+ """
349
372
self ._obj = obj
350
373
351
374
@wraps (method )
352
375
def __call__ (self , * args , ** kwargs ):
353
- return method (self ._obj , * args , ** kwargs )
376
+ """Call the accessor method.
377
+
378
+ Args:
379
+ *args: The arguments to pass to the registered method.
380
+ **kwargs: The keyword arguments to pass
381
+ to the registered method.
382
+
383
+ Returns:
384
+ object: The result of calling of the method.
385
+ """
386
+ global method_call_ctx_factory
387
+ if method_call_ctx_factory is None :
388
+ return method (self ._obj , * args , ** kwargs )
389
+
390
+ return handle_pandas_extension_call (
391
+ method , method_signature , self ._obj , args , kwargs
392
+ )
354
393
355
394
register_groupby_accessor (method .__name__ )(AccessorMethod )
356
395
return method
0 commit comments