Skip to content

Commit 69d45e1

Browse files
committed
added series support, lambda args support, wip
1 parent 3e6c6da commit 69d45e1

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pandas_flavor/register.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import ipdb
12
from functools import wraps
3+
import inspect
24
from pandas.api.extensions import register_series_accessor, register_dataframe_accessor
35
from . import stack_counter
46

57
handle_pandas_method_call = None
68

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+
718
def register_dataframe_method(method):
819
"""Register a function as a method attached to the Pandas DataFrame.
920
@@ -24,10 +35,20 @@ def __init__(self, pandas_obj):
2435
self._obj = pandas_obj
2536

2637
@wraps(method)
27-
def __call__(self, *args, **kwargs):
38+
def __call__(self, *args, **orig_kwargs):
2839
with stack_counter.global_scf.get_sc() as sc:
2940
#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+
3050
ret = method(self._obj, *args, **kwargs)
51+
3152
if sc.scf.level == 1:
3253
#print("pf dataframe __call__")
3354
global handle_pandas_method_call
@@ -54,7 +75,15 @@ def __init__(self, pandas_obj):
5475

5576
@wraps(method)
5677
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
5887

5988
register_series_accessor(method.__name__)(AccessorMethod)
6089

0 commit comments

Comments
 (0)