Hi I would like to subclass a pandas dataframe but the subclass of the dataframe will also inherit from a custom class of my own. I want to do this because I would like to make multiple subclassed dataframes, as well as other subclasses (that are not dataframes) that will share properties and methods of this base class.
To begin my base class is
class thing(object):
def __init__(self, item_location, name):
self.name = name
self.file = item_location
self.directory = os.path.join(*item_location.split(os.path.sep)[0:-1])
@property
def name(self):
return self._name
@name.setter
def name(self,val):
self._name = val
@property
def file(self):
return self._file
@file.setter
def file(self,val):
self._location = val
@property
def directory(self):
return self._directory
@directory.setter
def directory(self,val):
self._directory = val
And now one of my subclasses that will inherit from pandas and thing
class custom_dataframe(thing,pd.DataFrame):
def __init__(self, *args, **kwargs):
super(custom_dataframe,self).__init__(*args,**kwargs)
@property
def _constructor(self):
return custom_dataframe
I simply try to make a blank dataframe, and only give it name file location
custom_dataframe('/foobar/foobar/foobar.html','name')
and I get an error
(I cannot post the entire stack trace as its on a computer that is not connected to the internet)
File "<stdin>", line 1, in <module>
File "<path to file with classes>", line x, in __init__
self.name = name
<a bunch of stuff going through pandas library>
File "<path to pandas generic.py>", line 4372, in __getattr__
return object.__getattribute__(self,name)
RecursionError: maximum recursion depth exceeded while calling a Python object
I'm using pandas 0.23.4
edit:
changed item_location.split(os.pathsep)[0:-1] to *item_location.split(os.path.sep)[0:-1]
not have to change stuff in multiple places- composition should work - it may seem too bulky in simple cases but the effect decreases with increasing complexity. Anyway, in your case try to avoid simple attribute names likename,file, etc.