Skip to content

BUG: DataFrame attributes are lost after specifying dtype for select columns #44414

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
blaylockbk opened this issue Nov 12, 2021 · 5 comments · Fixed by #44680
Closed
2 of 3 tasks

BUG: DataFrame attributes are lost after specifying dtype for select columns #44414

blaylockbk opened this issue Nov 12, 2021 · 5 comments · Fixed by #44680
Assignees
Labels
Bug DataFrame DataFrame data structure metadata _metadata, .attrs
Milestone

Comments

@blaylockbk
Copy link

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

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

# I add attributes to a DataFrame
df = pd.DataFrame(dict(column1=[1,2,3,4], column2=[1,2,3,4]))
df.attrs['name'] = "hello"
print(df)
print(df.attrs)

OUT:
   column1  column2
0        1        1
1        2        2
2        3        3
3        4        4
{'name': 'hello'}

# When I change the dtype, the attribute is still there (as expected)
df1 = df.astype(float)
print(df1)
print(df1.attrs)

OUT:
   column1  column2
0      1.0      1.0
1      2.0      2.0
2      3.0      3.0
3      4.0      4.0
{'name': 'hello'}

# But when I specify the dtype for specific columns, the attributes are lost
df2 = df.astype({'column1':float})
print(df2)
print(df2.attrs)

OUT:
column1  column2
0      1.0        1
1      2.0        2
2      3.0        3
3      4.0        4
{}

Issue Description

I assigned attributes to a DataFrame. When I change the DataFrame dtype with df.astype(float), the DataFrame attribute are retained, as expected. But when I change the dtype for specific columns with df.astype({'column1':float}), the attributes are lost.

Expected Behavior

I expected the DataFrame to retrain the attributes after specifying the dtype for specific columns as it does when I specify the dtype for the full DataFrame.

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.18-24.78-default
Version : #1 SMP Mon Aug 2 21:00:23 UTC 2021 (a13100d)
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.4
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.2.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.0.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.0
IPython : 7.28.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fsspec : 2021.10.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : 2021.10.1
scipy : 1.7.1
sqlalchemy : 1.4.26
tables : None
tabulate : None
xarray : 0.19.0
xlrd : None
xlwt : None
numba : None

@blaylockbk blaylockbk added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 12, 2021
@phofl phofl added DataFrame DataFrame data structure metadata _metadata, .attrs labels Nov 12, 2021
@phofl
Copy link
Member

phofl commented Nov 12, 2021

This is related to #28283

@mroeschke mroeschke removed the Needs Triage Issue that has not been reviewed by a pandas team member label Nov 14, 2021
@skwirskj
Copy link
Contributor

I am new to the open-source community but would love to help and contribute to this issue!

@skwirskj
Copy link
Contributor

take

@skwirskj
Copy link
Contributor

@blaylockbk I have confirmed that this bug exists on the master branch of pandas

@skwirskj
Copy link
Contributor

I have a quick question about how I am planning to fix this bug. I am thinking of adding a finalize call to propogate the metadata right after line 5877 in generic.py:
[5877] result.columns = self.columns

Can anyone think of any issues this may bring up with performance? I know finalize is noted as sometimes taking up a bit of time compared to others, but I believe it is necessary to fix this issue.

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

Successfully merging a pull request may close this issue.

5 participants