Description
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