Reputation: 996
I am trying to create a class derived from pandas DataFrame. The class shall have each an attribute of type string
, DataFrame
and list
. There is no problem with the string
attribute, but DataFrame
and list
each cause a warning. Despite the warnings, the code seems to behave correctly.
Can anyone help me to fix my code? Or suppress the warnings?
Code:
import pandas as pd
class MyClass(pd.DataFrame):
def __init__(self, arg_string, arg_dataframe, *arg_params):
pd.DataFrame.__init__(self)
self._string = arg_string
self._dataframe = arg_dataframe
self._params = arg_params
if __name__=='__main__':
df = pd.DataFrame()
c1 = MyClass("test", df, 1, 2, 3)
print(c1._string)
print(c1._dataframe)
print(c1._params)
Warning message:
so_example.py:7: UserWarning: Pandas doesn't allow columns to be created via a new attribu
te name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
self._dataframe = arg_dataframe
so_example.py:8: UserWarning: Pandas doesn't allow columns to be created via a new attribu
te name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
self._params = arg_params
Stdout:
test
Empty DataFrame
Columns: []
Index: []
(1, 2, 3)
Upvotes: 1
Views: 1091
Reputation: 294536
You are attempting to subclass a dataframe.
I've answered how to subclass here -> LINK
Why have an attribute of a dataframe that is another dataframe? If you want an object that has several attributes, one of which is a dataframe, that's fine. Just don't define the class as class MyClass(pd.DataFrame):
but class MyClass(object):
instead.
Upvotes: 2