Reputation: 2652
I am trying to subclass Pandas' DataFrame
in another class, same like the GeoDataFrame
in GeoPandas
here.
However, I get a
maximum recursion depth exceeded error
when I run:
from pandas import DataFrame
df = pd.DataFrame({'A':[1,2,4], 'B':[4,5,6]})
class NewDataFrame(DataFrame):
def __init__(self, dataframe: pd.DataFrame) -> None:
self.dataframe = dataframe
super(NewDataFrame, self).__init__(dataframe)
def do_something(self):
print(self.dataframe.columns)
ndf = NewDataFrame(df)
full error
Traceback (most recent call last):
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-40-03d15755b527>", line 9, in <module>
ndf = NewDataFrame(df)
File "<ipython-input-40-03d15755b527>", line 3, in __init__
self.dataframe = dataframe
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 5092, in __setattr__
existing = getattr(self, name)
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 5065, in __getattr__
if self._info_axis._can_hold_identifiers_and_holds_name(name):
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 5065, in __getattr__
if self._info_axis._can_hold_identifiers_and_holds_name(name):
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 5065, in __getattr__
if self._info_axis._can_hold_identifiers_and_holds_name(name):
[Previous line repeated 1484 more times]
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 428, in _info_axis
return getattr(self, self._info_axis_name)
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 5063, in __getattr__
return object.__getattribute__(self, name)
File "pandas/_libs/properties.pyx", line 65, in pandas._libs.properties.AxisProperty.__get__
File "/Users/as/Documents/projects/model/.venv/lib/python3.7/site-packages/pandas/core/generic.py", line 5063, in __getattr__
return object.__getattribute__(self, name)
RecursionError: maximum recursion depth exceeded while calling a Python object
I am aware of this question and this, but answers to both seem what I exactly did unless I am missing something.
I am on Mac OS 10.14.5 and Python 3.7.3
Upvotes: 0
Views: 1904
Reputation: 51
You've got an infinite loop in your code. Maybe you've been looking for something like:
from pandas import DataFrame
class NewDataFrame(DataFrame):
def __init__(self, param) -> None:
super().__init__(param)
def do_something(self):
print(self.columns)
ndf = NewDataFrame({'A':[1,2,4], 'B':[4,5,6]})
Upvotes: 1