0

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

mallet
  • 2,454
  • 3
  • 37
  • 64
  • In the example you provided, the initialization argument isn't a dataframe instance like you're trying to do, just a string to define column. – ipaleka Aug 08 '19 at 12:26
  • Why are you calling the pd.Dataframe constructor twice? Why not call the pd.Dataframe with your dictionary directly ? `d = {'A':[1,2,4], 'B':[4,5,6]}` `class NewDataFrame(DataFrame): def __init__(self, dict: myDict) -> None: super(NewDataFrame,self).__init__(myDict)` – ma3oun Aug 08 '19 at 12:29
  • @ipaleka is it a string? I think it is a dataframe – mallet Aug 08 '19 at 12:46
  • @ma3oun the `df` is just an example, it isn't the actual dataframe I am trying to call into the class. – mallet Aug 08 '19 at 12:48
  • 1
    @salhin actually I was wrong, super is called even **without** those arguments, so only *args, **kwargs are provided to [DataFrame initialization](https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py). That means a dictionary is expected as the first positional argument or as `data` named argument. – ipaleka Aug 08 '19 at 12:55

1 Answers1

1

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]})
Zeecka
  • 51
  • 3
  • but how would I call `self.param` later in other functions? I have updated the example of my question to be more accurate – mallet Aug 08 '19 at 12:52
  • Actually, I figured out that using your answer, I can just call `param` as `self` instead of `self.param` and then it works fine. If you update your answer, I can mark it as accepted. – mallet Aug 08 '19 at 13:22