Skip to content

Series .attrs is not correctly maintained/propagated in to_frame #31452

Closed
@buhrmann

Description

@buhrmann

Hi, it seems that the .attrs dict (for storing metadata) is not always propagated correctly. E.g.

Code Sample

s = pd.Series([0,1,2], name="x")                                                                                                                                                                                                                                       
s.attrs["mydata"] = "test"                                                                                                                                                                                                                                             
s.attrs                                                                                                                                                                                                                                                                
# >> {'mydata': 'test'}

df = s.to_frame()                                                                                                                                                                                                                                                      
df.x.attrs                                                                                                                                                                                                                                                             
# >> {}

Problem description

As @jorisvandenbossche commented here, it seems that this is because there are a few places in pandas where calls to finalize are missing.

I ran into it mainly in the "expanddim" direction (from Series to DF). Copying, subsetting and manipulating a Dataframe column that has attrs explicitly set, in contrast, seems to work without losing the metadata. I haven't tried more complicated cases, like aggregations (I don't know much about pandas internals, but from previous attempts to implement metadata in my own subclass I seem to remember that these have special code paths in the BlockManager parts, where I suspect the same or related problem may occur...).

Expected Output

df.x.attrs                                                                                                                                                                                                                                                             
# >> {'mydata': 'test'}

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.0.final.0
python-bits : 64
OS : Darwin
OS-release : 19.2.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.0.0
numpy : 1.17.3
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0.post20200119
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.11.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions