Description
-
I have checked that this issue has not already been reported.
- Has sort of come up in other issues related to metadata/attrs not propagating through properly, however I haven't seen any issues specifically about
Series
attrs being lost when put into aDataFrame
.
- Has sort of come up in other issues related to metadata/attrs not propagating through properly, however I haven't seen any issues specifically about
-
I have confirmed this bug exists on the latest version of pandas.
-
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
See this GitHub Gist Notebook.
Summary of the issue is:
import pandas as pd
# Make some dummy dataframe
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
# Make some dummy series
col1_w_attr = df["col1"].copy()
# Add some attrs to it
col1_w_attr.attrs["test_attr"] = 1
df["col1_w_attr"] = col1_w_attr
# Issue 1: The attrs are missing, call `__finalize__` manually
df["col1_w_attr"].__finalize__(col1_w_attr)
# Repeat the above with another column:
col2_w_attr = df["col2"].copy()
col2_w_attr.attrs["test_attr"] = 2
df["col2_w_attr"] = col2_w_attr
df["col2_w_attr"].__finalize__(col2_w_attr)
# Attrs are present as before
df["col2_w_attr"].attrs
# Issue 2: col1_w_attrs is now missing its attrs:
df["col1_w_attr"].attrs
Problem description
First issue is that attrs are not propagated through when a Series
is added to a DataFrame
(likely related to #28283).
Second issue is that if you call __finalize__
to make sure that the attrs are present, once you add another column in or modify the dataframe, the attrs already present for a column are lost. My guess is that when the DataFrame is modified, finalize is not called per-column again, but I haven't looked into it much.
I'd be happy to help with this issue, however there are already many issues related to this topic and a few branches where others are working on attrs/metadata/finalize, so I'm a bit hesitant to start work as it'll probably conflict with what has already been done.
What would be the best place to discuss collaboration on this?
Expected Output
Expectation would be that you can attach some attrs to a Series, place it into a dataframe, modify/work with the dataframe, then access the attrs again.
Output of pd.show_versions()
import pandas as pd
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.7.0-3-MANJARO
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.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