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: pd.DataFrame.sum with min_count changes dtype if result contains NaNs #46947

Closed
2 of 3 tasks
kdebrab opened this issue May 5, 2022 · 2 comments · Fixed by #47091
Closed
2 of 3 tasks

BUG: pd.DataFrame.sum with min_count changes dtype if result contains NaNs #46947

kdebrab opened this issue May 5, 2022 · 2 comments · Fixed by #47091
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reduction Operations sum, mean, min, max, etc.
Milestone

Comments

@kdebrab
Copy link
Contributor

kdebrab commented May 5, 2022

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 numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1., 2.3, 4.4], 'b': [2.2, 3, np.nan]}, dtype='float32')
>>> df.sum(axis=1, min_count=1).dtype
dtype('float32')  # OK
>>> df.sum(axis=1, min_count=2).dtype
dtype('float64')  # not OK
>>> df.sum(axis=1, skipna=False).dtype
dtype('float32')  # OK

Issue Description

When using min_count, the dtype of the result depends on whether the result contains a NaN or not.

Expected Behavior

The resulting dtype should be the same as the dtype of the original DataFrame, also when the result contains NaNs.

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.10.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 1.4.2
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 22.0.4
setuptools : 58.1.0
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.1
IPython : 8.1.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.4
brotli : None
fastparquet : None
fsspec : 2022.02.0
gcsfs : None
markupsafe : 2.1.0
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.32
tables : None
tabulate : 0.8.9
xarray : 2022.3.0
xlrd : None
xlwt : None
zstandard : None

@kdebrab kdebrab added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2022
@Anupam-USP
Copy link
Contributor

I want to take this issue. Is there any reference to take help?

@simonjayhawkins
Copy link
Member

thanks @Anupam-USP

things look good upto the _maybe_null_out call from nansum in pandas/core/nanops.py

the_sum = values.sum(axis, dtype=dtype_sum)
the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count)
return the_sum

where the code sample for the failing case gets cast to float64 here

result = result.astype("f8")

@simonjayhawkins simonjayhawkins added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reduction Operations sum, mean, min, max, etc. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 18, 2022
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone May 18, 2022
@simonjayhawkins simonjayhawkins added the Dtype Conversions Unexpected or buggy dtype conversions label May 18, 2022
@jreback jreback modified the milestones: Contributions Welcome, 1.5 May 29, 2022
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 Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants