1

I have a class that extends pandas

class teste(pd.DataFrame):

def __init__(self, data=None, index=None, columns=None, dtype=None, 
             copy=False, atrib_0 = '', atrib_1 = None, atrib_2 = []):

    super(teste,self).__init__(data=data, index=index, columns=columns, dtype=dtype, copy=copy)

    self.atrib_0 = atrib_0
    self.atrib_1 = atrib_1
    self.atrib_2 = atrib_2

    return

I created an instance of that class using the following code:

t = teste(pandas_df,
                atrib_0 = 'NAME',
                atrib_1 = 'D',
                atrib_2 = ['A','B','C','D'],
                )

But doing that generates a UserWarning for the atrib_2, saying Pandas doesn't allow columns to be created via a new attribute name.

Since I am not creating a new column, but attributing a property to that instance of my class, I believe it gets confused because it's possible to access existing columns using the code df.new_column = []. Any new attribute that gets a list generates that warning.

Does anybody know how to get rid of it? What am I doing wrong? Any help is much appreciated.

Gaduks
  • 671
  • 6
  • 13
  • 1
    You can't use dot notation to _create_ a new column, only bracket notation. Once the column is created, _then_ you can use dot notation to access it. See this [SO answer](https://stackoverflow.com/questions/41130255/in-a-pandas-dataframe-whats-the-difference-between-using-squared-brackets-or-d/41130329#41130329) and this [repo issue](https://github.com/pandas-dev/pandas/issues/7175) for a more in-depth discussion. – dmitriys Nov 23 '18 at 21:26
  • Yes, my mistake when I commented about why I though the warning was showing. I'll edit the question. – Gaduks Nov 23 '18 at 21:29
  • It's just a warning. It works as expected right? – ayhan Nov 23 '18 at 21:40
  • Yes, I believe it's just the warning, I encountered no other issues so far. But I think there must be a way to do it without generating it. And I am not sure, but I think I must not be doing it right, since the warning was generated. – Gaduks Nov 23 '18 at 21:47

0 Answers0