【发布时间】:2019-08-26 15:31:01
【问题描述】:
我想继承pandas DataFrame,并用一个空的dataframe初始化子类
import pandas as pd
class MyChildDataFrame(pd.DataFrame):
@property
def _constructor(self):
return MyChildDataFrame
def __init__(self, *args, **kwargs):
print("Iniciando " + self.__class__.__name__)
super(pd.DataFrame, self).__init__(*args, **kwargs)
最后一行产生错误
init() 缺少 1 个必需的位置参数:“数据”
当使用空数据创建类时
x=MyChildDataFrame()
print(x)
我已经阅读了pandas documentation on inheriting from DataFrame,但它没有解释如何调用 init 方法。
我也尝试将 self 作为参数传递:
super(pd.DataFrame, self).__init__(self, *args, **kwargs)
并且该行执行,但是在尝试打印数据帧时,它会创建大量错误并导致 Visual Studio 调试器崩溃
当我运行这段代码时:
x=MyChildDataFrame()
print(x)
我期待这个输出
Empty DataFrame
Columns: []
Index: []
但我收到错误:
Traceback (most recent call last):
File "c:\program files (x86)\microsoft visual studio\2017\professional\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 688, in trace_dispatch
if main_debugger.in_project_scope(frame.f_code.co_filename):
The program 'python.exe' has exited with code -1 (0xffffffff).
【问题讨论】: