Python 超级:超过最大递归深度
我试图在另一个类中对Pandas的DataFrame进行子类化,就像GeoPandas中的GeoDataFrame一样 然而,我得到了一份工作 超过最大递归深度错误 当我跑步时:Python 超级:超过最大递归深度,python,pandas,oop,superclass,Python,Pandas,Oop,Superclass,我试图在另一个类中对Pandas的DataFrame进行子类化,就像GeoPandas中的GeoDataFrame一样 然而,我得到了一份工作 超过最大递归深度错误 当我跑步时: from pandas import DataFrame df = pd.DataFrame({'A':[1,2,4], 'B':[4,5,6]}) class NewDataFrame(DataFrame): def __init__(self, dataframe: pd.DataFrame) ->
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)
完全错误
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
我知道和,但这两个问题的答案似乎都是我所做的,除非我遗漏了什么
我使用的是Mac OS 10.14.5和Python 3.7.3,代码中有一个无限循环。也许你一直在寻找这样的东西: 从导入数据帧 类NewDataFrameDataFrame: 定义初始化自身,参数->无: 超级参数 定义做什么自己: printself.columns ndf=新数据帧{'A':[1,2,4],'B':[4,5,6]}
在您提供的示例中,initialization参数不像您正在尝试的那样是dataframe实例,而是定义列的字符串。为什么要调用pd.dataframe构造函数两次?为什么不直接用字典调用pd.Dataframe呢?d={'A':[1,2,4],'B':[4,5,6]}类NewDataFrameDataFrameDataFrame:def_uinit_uself,dict:myDict->None:superNewDataFrame,self.\uu init__myDict@ipaleka它是一根绳子吗?我认为这是一个错误dataframe@ma3oundf只是一个例子,它不是我试图调用到类中的实际数据帧。@salhin实际上我错了,super甚至在没有这些参数的情况下被调用,所以只提供了*args、**kwargs。这意味着字典应作为第一个位置参数或名为argument的数据。但以后在其他函数中如何调用self.param?我已经更新了我的问题的示例,使其更加准确。事实上,我发现使用您的答案,我可以将param称为self而不是self.param,然后它就可以正常工作了。如果您更新您的答案,我可以将其标记为已接受。