如何将属性添加到pandas.DataFrame的子类?

时间:2012-11-05 10:00:57

标签: pandas

我想将属性添加到DataFrame的子类中,但是我收到错误:

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


RuntimeError: maximum recursion depth exceeded

2 个答案:

答案 0 :(得分:2)

您想要写如下:

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

请参阅Python's __init__ syntax问题。

答案 1 :(得分: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