8
8
9
9
method_call_ctx_factory = None
10
10
11
+
11
12
def handle_pandas_extention_call (method , method_signature , obj , args , kwargs ):
12
13
"""
13
14
This function is called when the user calls the registered method on pandas dataframe object.
@@ -19,7 +20,7 @@ def handle_pandas_extention_call(method, method_signature, obj, args, kwargs):
19
20
In this case the implementation calls the registered method with unmodified args and kwargs and returns underlying method result.
20
21
21
22
b) case when *method_call_ctx_factory* is not None
22
- In this case *method_call_ctx_factory* expected to refer to the function to create the context object. The context object will be used
23
+ In this case *method_call_ctx_factory* expected to refer to the function to create the context object. The context object will be used
23
24
to process inputs and outputs of *method* call. It is also possible that the context object method *handle_start_method_call*
24
25
will modify original args and kwargs before *method* call.
25
26
@@ -39,11 +40,11 @@ def handle_end_method_call(self, ret: object) -> None: raise NotImplemented
39
40
40
41
Parameters
41
42
----------
42
- method :
43
+ method :
43
44
method object as registered by decorator register_dataframe_method (or register_series_method)
44
- method_signature :
45
+ method_signature :
45
46
signature of method as returned by inspect.signature
46
- obj :
47
+ obj :
47
48
pandas object - Dataframe or Series
48
49
*args : list
49
50
The arguments to pass to the registered method.
@@ -67,10 +68,7 @@ def handle_end_method_call(self, ret: object) -> None: raise NotImplemented
67
68
with method_call_ctx :
68
69
if not isinstance (method_call_ctx , nullcontext ):
69
70
all_args = tuple ([obj ] + list (args ))
70
- (
71
- new_args ,
72
- new_kwargs ,
73
- ) = method_call_ctx .handle_start_method_call (
71
+ (new_args , new_kwargs ,) = method_call_ctx .handle_start_method_call (
74
72
method .__name__ , method_signature , all_args , kwargs
75
73
)
76
74
args = new_args [1 :]
@@ -81,7 +79,8 @@ def handle_end_method_call(self, ret: object) -> None: raise NotImplemented
81
79
if not isinstance (method_call_ctx , nullcontext ):
82
80
method_call_ctx .handle_end_method_call (ret )
83
81
84
- return ret
82
+ return ret
83
+
85
84
86
85
def register_dataframe_method (method ):
87
86
"""Register a function as a method attached to the Pandas DataFrame.
@@ -106,7 +105,9 @@ def __init__(self, pandas_obj):
106
105
107
106
@wraps (method )
108
107
def __call__ (self , * args , ** kwargs ):
109
- return handle_pandas_extention_call (method , method_signature , self ._obj , args , kwargs )
108
+ return handle_pandas_extention_call (
109
+ method , method_signature , self ._obj , args , kwargs
110
+ )
110
111
111
112
register_dataframe_accessor (method .__name__ )(AccessorMethod )
112
113
@@ -129,7 +130,9 @@ def __init__(self, pandas_obj):
129
130
130
131
@wraps (method )
131
132
def __call__ (self , * args , ** kwargs ):
132
- return handle_pandas_extention_call (method , method_signature , self ._obj , args , kwargs )
133
+ return handle_pandas_extention_call (
134
+ method , method_signature , self ._obj , args , kwargs
135
+ )
133
136
134
137
register_series_accessor (method .__name__ )(AccessorMethod )
135
138
0 commit comments