Skip to content

BUG: Groupby then sum boolean column gives too large a sum #50347

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
3 tasks done
agaventa opened this issue Dec 19, 2022 · 6 comments · Fixed by #50488
Closed
3 tasks done

BUG: Groupby then sum boolean column gives too large a sum #50347

agaventa opened this issue Dec 19, 2022 · 6 comments · Fixed by #50488
Assignees
Labels
Groupby Needs Tests Unit test(s) needed to prevent regressions Upstream issue Issue related to pandas dependency

Comments

@agaventa
Copy link

agaventa commented Dec 19, 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 pandas as pd

n = 32

df = pd.DataFrame({'label': [1]*n,'bool_column': [True]*(n)})

print(df.assign(bool_column_dupe=(df.bool_column==True)).groupby("label").bool_column_dupe.sum())

Issue Description

Summing a boolean column after a groupby gives an unreasonably large and incorrect sum.
This is the result from the above code:
1 8128 Name: bool_column_dupe, dtype: int64

The issue does not occur if I instead add an astype(int):
print(df.assign(bool_column_dupe(df.bool_column==True).astype(int)).groupby("label").bool_column_dupe.sum())

The issue does not occur if I don't use the .assign:
print(df.groupby("label").bool_column.sum())

The issue does not occur with n<32.

The issue only occurs with numpy v1.24.0.
It does not occur with numpy v1.23.5.

Expected Behavior

Expected to see:
1 32 Name: bool_column_dupe, dtype: int64

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.9.13.final.0
python-bits : 64
OS : Darwin
OS-release : 22.1.0
Version : Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_GB.UTF-8
pandas : 1.5.2
numpy : 1.24.0
pytz : 2022.7
dateutil : 2.8.2
setuptools : 60.2.0
pip : 21.3.1
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 : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@agaventa agaventa added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 19, 2022
@asishm
Copy link
Contributor

asishm commented Dec 19, 2022

this might be a numpy issue

In [77]: a = np.array([True] * 32)

In [78]: b = (a == True)

In [79]: b.view('uint8')
Out[79]:
array([254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
       254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254,
       254, 254, 254, 254, 254, 254], dtype=uint8)

In [80]: np.__version__
Out[80]: '1.24.0'

@phofl
Copy link
Member

phofl commented Dec 19, 2022

This works on main for me, so might need tests.

We made quite a lot of changes on groupby algorithms to preserve dtypes better, so could be related to that

@phofl phofl added Groupby good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 19, 2022
@asishm
Copy link
Contributor

asishm commented Dec 20, 2022

I don't think this works on main

In [3]: pd.__version__, np.__version__
Out[3]: ('2.0.0.dev0+971.gca3e0c875f', '1.24.0')

In [4]: n = 32
   ...: df = pd.DataFrame({'label': 1, 'a': [True] * n})
   ...: df['b'] = df['a'] == True
   ...: df.groupby('label').sum()
Out[4]:
        a     b
label
1      32  8128

@phofl
Copy link
Member

phofl commented Dec 20, 2022

Hm good point, did not use numpy 1.24.0
I agree that this looks like a numpy bug

@agaventa
Copy link
Author

This is now fixed in numpy 1.24.1

@Daquisu
Copy link
Contributor

Daquisu commented Dec 29, 2022

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby Needs Tests Unit test(s) needed to prevent regressions Upstream issue Issue related to pandas dependency
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants