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)
Juergen
  • 956
  • 1
  • 9
  • 24

1 Answers1

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.

piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 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? – Juergen Aug 31 '18 at 18:22
  • Probably. But I think you can accomplish your task if you pass the name of your attributes into the `_metadata` class variable. – piRSquared Aug 31 '18 at 18:24