Skip to content
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

BUG: DataFrame.stack() sometimes changes dtype from float32 to float64 #51059

Closed
2 of 3 tasks
torfsen opened this issue Jan 30, 2023 · 2 comments
Closed
2 of 3 tasks

BUG: DataFrame.stack() sometimes changes dtype from float32 to float64 #51059

torfsen opened this issue Jan 30, 2023 · 2 comments
Assignees
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@torfsen
Copy link

torfsen commented Jan 30, 2023

Pandas version checks

  • 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 main branch of pandas.

Reproducible Example

import pandas as pd

df1 = pd.DataFrame({("n1", "q1"): [1], ("n2", "q1"): [2]}, dtype="float32")
print(df1.stack(level=0).dtypes)  # q1 is float32

df2 = pd.DataFrame({("n1", "q1"): [1], ("n2", "q2"): [2]}, dtype="float32")
print(df2.stack(level=0).dtypes)  # q1 and q2 are float64

Issue Description

In some cases, DataFrame.stack will "change" the dtype of the resulting columns, in my case from float32 to float64.

Expected Behavior

I would expect DataFrame.stack() to return a float32 DataFrame if the input DataFrame was also float32.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.8.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-104-generic
Version : #118-Ubuntu SMP Wed Mar 2 19:02:41 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.23.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 67.0.0
pip : 22.3.1
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : 5.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 8.7.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@torfsen torfsen added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 30, 2023
@phofl phofl added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Dtype Conversions Unexpected or buggy dtype conversions and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 2, 2023
@lithomas1 lithomas1 self-assigned this Feb 6, 2023
@lithomas1
Copy link
Member

Thanks for reporting this issue.
I can reproduce this, gonna take a look this week.

@rhshadrach
Copy link
Member

With future_stack=True, the result is now float32. Closed by #55448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
4 participants