BUG: DataFrame.stack() sometimes changes dtype from float32 to float64 · Issue #51059 · pandas-dev/pandas (original) (raw)
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