【问题标题】:Error trying to inherit from pandas DataFrame, when calling __init__调用 __init__ 时尝试从 pandas DataFrame 继承时出错
【发布时间】: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).

【问题讨论】:

    标签: python-3.x visual-studio


    【解决方案1】:
    super().__init__(*args, **kwargs)
    

    似乎有效,但我不知道我是否正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-23
      • 2016-09-22
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多