My goal is to create an object that behaves the same as a Pandas DataFrame, but with a few extra methods of my own on top of it. As far as I understand, one approach would be to extend the class, which I first tried to do as follows:
class CustomDF(pd.DataFrame):
def __init__(self, filename):
self = pd.read_csv(filename)
But I get errors when trying to view this object, saying: 'CustomDF' object has no attribute '_data'
.
My second iteration was to instead not inherit the object, but rather import it as a DataFrame into one of the object attributes, and have the methods work around it, like this:
class CustomDF():
def __init__(self, filename):
self.df = pd.read_csv(filename)
def custom_method_1(self,a,b,...):
...
def custom_method_2(self,a,b,...):
...
This is fine, except that for all custom methods, I need to access the self.df
attribute first to do anything on it, but I would prefer that my custom dataframe were just self
.
Is there a way that this can be done? Or is this approach not ideal anyway?