Skip to content

BUG: when attrs lost on original dataframe, copy loses the attrs as well #41572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
3 tasks done
snowman2 opened this issue May 19, 2021 · 1 comment
Open
3 tasks done
Labels
Bug metadata _metadata, .attrs

Comments

@snowman2
Copy link

snowman2 commented May 19, 2021

  • I have checked that this issue has not already been reported.

  • 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.


Possibly related to:

Code Sample, a copy-pastable example

df = pandas.DataFrame({"a": [1]})
df.a.attrs = {"test": "test"}
df2 = df.copy()  # same behavior for  df.reset_index()
df2.a.attrs
{}
df.a.attrs
{}

Problem description

Ideally, when making a copy, the attributes will be preserved on both dataframes.

Expected Output

df2.a.attrs
 {"test": "test"}
df.a.attrs
 {"test": "test"}

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 1963b81
python : 3.7.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-73-generic
Version : #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.0.dev0+1660.g1963b810b3
numpy : 1.20.1
pytz : 2020.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.1.0
Cython : 0.29.23
pytest : 6.2.2
hypothesis : 6.13.0
sphinx : 3.5.4
blosc : 1.10.2
feather : None
xlsxwriter : 1.4.3
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 2021.05.0
fastparquet : 0.6.3
gcsfs : 2021.05.0
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.0
pyxlsb : None
s3fs : 2021.05.0
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.17.1.dev149+g49aa235f.d20210518
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.53.1

@snowman2 snowman2 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 19, 2021
@mroeschke mroeschke added metadata _metadata, .attrs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 20, 2021
@louvervecken
Copy link

I had the same issue and just opened a stackoverflow question about it.
The DataFrame attrs (empty in your example) actually seems to overwrite the Series attrs.

This seems like a serious bug as not only the result is not correct but also the instance where is copied from gets mutated. Is there any work being planned or done on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug metadata _metadata, .attrs
Projects
None yet
Development

No branches or pull requests

3 participants