1

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)

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I think that link will help a lot. I want an obejct, that behaves like a dataframe and has private members, some of which are again dataframes (or subclasses of dataframe). Is this conceptually bad style?
Probably. But I think you can accomplish your task if you pass the name of your attributes into the _metadata class variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.