Reputation: 6713
I am trying to write a class that inherits pandas' DataFrame
class for some custom data that I am working on.
class EquityDataFrame(DataFrame):
def __init__(self, *args, **kwargs):
DataFrame.__init__(self, *args, **kwargs)
def myfunc1(self,...)
... # does something
def myfunc2(self, ...)
... # does something
In PyCharm, I get the following warning for the name of the class EquityDataFrame
:
Class EquityDataFrame must implement all abstract methods
This inspection detects when there aren't all abstract properties/methods defined in the subclass
I tried googling, but I couldn't find a useful description of this warning. Can someone help me what this warning means?
Upvotes: 6
Views: 2584
Reputation: 301
from pandas import DataFrame
class PrepPandas(DataFrame):
"""Class that inherits from pandas.DataFrame then customizes it with additonal methods."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def _constructor(self):
"""
Creates a self object that is basically a pandas.Dataframe.
self is a dataframe-like object inherited from pandas.DataFrame
self behaves like a dataframe + new custom attributes and methods.
"""
return PrepPandas
def mult2(self):
"""
Method to demo chanining multiple function calls. Ex: pdf.mult2().mult2()
Example:
pdf = PrepPandas({'a': [1,2,3], 'b':[4,5,6]})
print(pdf, pdf.mult2(), pdf.mult2().mult2(), pdf.__class__, pdf.mult2().__class__)
"""
return self * 2
pdf = PrepPandas({'a': [1,2,3], 'b':[4,5,6]}) # Creates a pandas df.
pdf, pdf.mult2(), pdf.mult2().mult2(), pdf.__class__, pdf.mult2().__class__ # Adds a cutom mult2 function to the pdf.
Out[5]:
(
a b
0 1 4
1 2 5
2 3 6,
a b
0 2 8
1 4 10
2 6 12,
a b
0 4 16
1 8 20
2 12 24,
__main__.PrepPandas,
__main__.PrepPandas
)
Upvotes: 0
Reputation: 161
After looking for 6 years, pylint gave me the answer :
[W0223(abstract-method), CustomDataFrame] Method '_constructor_expanddim' is abstract in class 'DataFrame' but is not overridden
And indeed implementing
@property
def _constructor_expanddim(self) -> Type["CustomDataFrame"]:
raise NotImplementedError("Not supported for CustomDataFrames!")
made the warning disappear.
Upvotes: 3
Reputation: 468
Try something like the below
import pandas as pd
class MyDataframe(pd.DataFrame):
def __init__(self, *args, **kwargs):
super(MyDataframe, self).__init__(*args, **kwargs)
@property
def function_1(self, x):
@property
def function_2(self):
Upvotes: 0