1
+ import ipdb
1
2
from functools import wraps
3
+ import inspect
2
4
from pandas .api .extensions import register_series_accessor , register_dataframe_accessor
3
5
from . import stack_counter
4
6
5
7
handle_pandas_method_call = None
6
8
9
+ class LambdaCall :
10
+ def __init__ (self , func ):
11
+ self .func = func
12
+ self .ret = None
13
+
14
+ def __call__ (self , * args , ** kwargs ):
15
+ self .ret = self .func (* args , ** kwargs )
16
+ return self .ret
17
+
7
18
def register_dataframe_method (method ):
8
19
"""Register a function as a method attached to the Pandas DataFrame.
9
20
@@ -24,10 +35,20 @@ def __init__(self, pandas_obj):
24
35
self ._obj = pandas_obj
25
36
26
37
@wraps (method )
27
- def __call__ (self , * args , ** kwargs ):
38
+ def __call__ (self , * args , ** orig_kwargs ):
28
39
with stack_counter .global_scf .get_sc () as sc :
29
40
#ipdb.set_trace()
41
+ kwargs = {}
42
+ for k , arg in orig_kwargs .items ():
43
+ if method .__name__ == 'assign' and inspect .isfunction (arg ):
44
+ print ('got got that' )
45
+ #ipdb.set_trace()
46
+ kwargs [k ] = LambdaCall (arg )
47
+ else :
48
+ kwargs [k ] = arg
49
+
30
50
ret = method (self ._obj , * args , ** kwargs )
51
+
31
52
if sc .scf .level == 1 :
32
53
#print("pf dataframe __call__")
33
54
global handle_pandas_method_call
@@ -54,7 +75,15 @@ def __init__(self, pandas_obj):
54
75
55
76
@wraps (method )
56
77
def __call__ (self , * args , ** kwargs ):
57
- return method (self ._obj , * args , ** kwargs )
78
+ with stack_counter .global_scf .get_sc () as sc :
79
+ ret = method (self ._obj , * args , ** kwargs )
80
+ if sc .scf .level <= 2 :
81
+ #ipdb.set_trace()
82
+ print ("pf series __call__" )
83
+ global handle_pandas_method_call
84
+ if handle_pandas_method_call :
85
+ handle_pandas_method_call (self ._obj , method .__name__ , args , kwargs , ret )
86
+ return ret
58
87
59
88
register_series_accessor (method .__name__ )(AccessorMethod )
60
89
0 commit comments