I want to inherith from pandas DataFrame, and initialize the child class with an empty 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)
The last line creates an error
init() missing 1 required positional argument: 'data'
when the class is created with empty data
x=MyChildDataFrame()
print(x)
I had read the pandas documentation on inheriting from DataFrame, but it does not explains how to call the init method.
I also tried to pass self as parameter:
super(pd.DataFrame, self).__init__(self, *args, **kwargs)
And the line executes, but when trying to print the dataframe, but it creates a large list of error and crashes the Visual Studio debugger
When I run this code:
x=MyChildDataFrame()
print(x)
I expect this output
Empty DataFrame
Columns: []
Index: []
but I get errors:
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).
super().__init__(*args, **kwargs)
seems to work, but I don't know if I doing it correctly.
User contributions licensed under CC BY-SA 3.0