Skip to content

BUG: GroupBy sum on an Int64 column with min_count=1 is wrong when all values are null #34172

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
ali-tny opened this issue May 14, 2020 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Duplicate Report Duplicate issue or pull request
Milestone

Comments

@ali-tny
Copy link

ali-tny commented May 14, 2020

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


import pandas as pd

df = pd.DataFrame(
    [
        {"key": 1, "value": None},
    ],
    dtype="Int64",
)

>>> df["value"].sum(min_count=1)
<NA>
>>> df.groupby("key")["value"].sum(min_count=1)
key
1    -9223372036854775808
Name: value, dtype: Int64

Problem description

When summing with min_count=1 (so that groups with all nulls sum to null), DataFrame.sum exhibits expected behaviour (ie, all nulls sum to null), but GroupBy.sum returns the minimum Int64 -9223372036854775808 rather than <NA>.

Expected Output

>>> df.groupby("key")["value"].sum(min_count=1)
key
1    <NA>
Name: value, dtype: Int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.6.final.0
python-bits : 64
OS : Darwin
OS-release : 17.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.3
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.14.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@ali-tny ali-tny added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 14, 2020
@jreback
Copy link
Contributor

jreback commented May 14, 2020

pls try on master
a patch was recently merged for this

@jorisvandenbossche
Copy link
Member

Yes, master gives

In [21]: import pandas as pd 
    ...:  
    ...: df = pd.DataFrame( 
    ...:     [ 
    ...:         {"key": 1, "value": None}, 
    ...:     ], 
    ...:     dtype="Int64", 
    ...: )                                                                                                                                                                                                         

In [22]: df.groupby("key")["value"].sum(min_count=1)                                                                                                                                                               
Out[22]: 
key
1    <NA>
Name: value, dtype: Int64

Duplicate of #32861

@jorisvandenbossche jorisvandenbossche added Duplicate Report Duplicate issue or pull request and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 14, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.1 milestone May 14, 2020
@ali-tny
Copy link
Author

ali-tny commented May 15, 2020

Thank you! Sorry I missed the closed issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

3 participants