4

我想将属性添加到 DataFrame 的子类,但出现错误:

>>> import pandas as pd
>>>class Foo(pd.DataFrame):
...     def __init__(self):
...         self.bar=None
...         
>>> Foo()


RuntimeError: maximum recursion depth exceeded
4

2 回答 2

2

你想这样写:

class Foo(pd.DataFrame):
  def __init__(self):
    super(Foo, self).__init__()
    self.bar = None

请参阅Python 的__init__语法问题。

于 2012-11-05T11:45:37.037 回答
1
In [12]: class Foo(pd.DataFrame):
   ....:     def __init__(self, bar=None):
   ....:         super(Foo, self).__init__()
   ....:         self.bar = bar      

这导致: -

In [30]: my_special_dataframe = Foo(bar=1)

In [31]: my_special_dataframe.bar
Out[31]: 1

In [32]: my_special_dataframe2 = Foo() 

In [33]: my_special_dataframe2.bar   
于 2012-11-05T11:48:41.307 回答