I was trying to include the outside defined functions inside the class. I have made a lot of pandas manipulation functions and they run fine, but when I tried to add them in Class, I got the problem. Here is my outline:
MWE
import numpy as np
import pandas as pd
df = pd.DataFrame({'A': [1,2,3],'B':[10,20,30],
'C':[100,200,300],'D':[1000,2000,3000]})
@pd.api.extensions.register_dataframe_accessor("my")
class MyAccessor:
def __init__(self, pandas_obj):
self._obj = pandas_obj
def add_1_2(self,col1,col2):
df = self._obj
return df[col1] + df[col2]
df.my.add_1_2('A','B')
This works (but I have already functions defined without self._obj)
# this works
def add_1_3(self,col1,col2,col3):
df = self._obj
return df[col1] + df[col3]
MyAccessor.add_1_3 = add_1_3
df.my.add_1_3('A','B','C')
My attempt
def temp_fn(df,col1,col2,mydict):
print(mydict)
return df[col1]+df[col2]
col1,col2,mydict = 'A','B',{'lang':'python'}
temp_fn(df,col1,col2,mydict) # calling directly using function works good
# now I want to include this function inside the Class
def make_class_fn(fn, *args, **kwargs):
return fn(args[0]._obj, *args[1:], **kwargs)
MyAccessor.temp_fn = make_class_fn(temp_fn,df,col1,col2,mydict)
AttributeError: 'DataFrame' object has no attribute '_obj'
Note
If I had only one function, I could copy the same function inside the Class and edit it there, but I have a large number of functions and that's not a great idea.