Skip to content

BUG: df.copy removes attributes from series in the original dataframe #38107

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

Closed
2 of 3 tasks
ivirshup opened this issue Nov 27, 2020 · 2 comments
Closed
2 of 3 tasks

BUG: df.copy removes attributes from series in the original dataframe #38107

ivirshup opened this issue Nov 27, 2020 · 2 comments
Labels

Comments

@ivirshup
Copy link
Contributor

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


Code Sample, a copy-pastable example

# Your code here
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3]})
df["a"].attrs["key"] = "value"
display(df["a"].attrs)
# {'key': 'value'}
_ = df.copy()
display(df["a"].attrs)
# {}

Problem description

Calling copy on a dataframe probably shouldn't modify it or it's contents (especially those exposed through public attributes) in any way.

I would also ague the copy should keep these attributes, but that's more related to #35425

Expected Output

The second display(df["a"].attrs) should print: {'key': 'value'}

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 67a3d4241ab84419856b84fc3ebc9abcbe66c6b3
python           : 3.8.5.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 19.6.0
Version          : Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.1.4
numpy            : 1.19.4
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 20.2.4
setuptools       : 50.3.2
Cython           : 0.29.21
pytest           : 6.1.2
hypothesis       : None
sphinx           : 3.3.1
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : 7.19.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fsspec           : 0.7.4
fastparquet      : 0.4.1
gcsfs            : None
matplotlib       : 3.3.3
numexpr          : 2.7.1
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 2.0.0
pytables         : None
pyxlsb           : None
s3fs             : 0.4.2
scipy            : 1.5.4
sqlalchemy       : 1.3.18
tables           : 3.6.1
tabulate         : 0.8.7
xarray           : 0.16.1
xlrd             : 1.2.0
xlwt             : None
numba            : 0.51.2
@ivirshup ivirshup added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 27, 2020
@leonarduschen
Copy link
Contributor

I believe this is intentional. .attrs is only propagated on the object itself (i.e. the dataframe) and not on each individual columns.

This is related to #35425

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Nov 27, 2020

Indeed, this is a consequence of what is being discussed in #35425.

The reason that it "seems" to retain the attrs for the Series before copying, is because we cache the Series upon first access. So you are actually setting the attrs on this cached Series. You can see that if we clear the cache, the attrs are not hold on the DataFrame:

In [12]: df = pd.DataFrame({"a": [1, 2, 3]})
    ...: df["a"].attrs["key"] = "value"

In [13]: df["a"].attrs
Out[13]: {'key': 'value'}

In [14]: df._clear_item_cache()

In [15]: df["a"].attrs
Out[15]: {}

So let's discuss this further as part of #35425

@jorisvandenbossche jorisvandenbossche removed the Needs Triage Issue that has not been reviewed by a pandas team member label Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants