'Add custom attribute to Pandas.Series

I'm trying to add a single custom attribute to each pd.Series inside of a pd.DataFrame. Specifically, I'm getting a CSV where, intermittently, color codes are embedded in the column headers. I would like to pre-process those out into an attribute before graphing - and assign default colors to other non-specified columns.

But at its core, I just need a custom attribute tucked into the Series somewhere, like you might on any other Python object. Simplified example:

>>> import pandas as pd
>>> df = pd.DataFrame({"Low":[1,2,3], "Medium":[4,5,6], "High":[7,8,9]})
>>> s1 = df.iloc[:,1]
>>> 
>>> s1.color = 'yellow'
>>> print(s1.color)
yellow
>>>
>>> type(s1)
<class 'pandas.core.series.Series'>
>>>
>>> ### assign back to the DataFrame...
>>> df.iloc[:,1] = s1
>>>
>>> print(df.iloc[:,1].color)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py", line 5487, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'color' 
>>>
>>>
>>> ### Drat... maybe assigning directly to the Series object:
>>> setattr(df.iloc[:,1], 'color', 'yellow')
>>> 
>>> ### goes in ok, but...
>>> df.iloc[:,1].color
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/generic.py", line 5487, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'color'
>>> 

Obviously pandas isn't just passing around pointers. I don't need to serialize, but I do need the attribute to survive being passed around between runtime functions.

I'd rather not subclass the entire dataframe/series model just for one attribute. Tired of bang my head on this, I'm aiming at a wrapper class for the dataframe that stores an attribute map between color and series index, but... again, seems like a lot of clutter for +1 attribute on a python object.

Any thoughts on the simplest / cleanest solution?



Solution 1:[1]

I think my answer in a similar question could help you. What I think you need is a custom attribute accessor to extend Pandas Series

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sbunzini