BUG: unexpected results for idxmax groupby aggregation on uint column · Issue #57040 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

This reproducer:

import numpy as np import pandas as pd key = np.array([0, 0, 1, 1, 2, 2, 3, 3]) val = np.array([0, 9, 0, 0, 0, 0, 0, 0], dtype=np.uint64) df = pd.DataFrame({"key": key, "val": val}) df.groupby("key").idxmax()

Gives the following:

But if we switch val to be int64

import numpy as np import pandas as pd key = np.array([0, 0, 1, 1, 2, 2, 3, 3]) val = np.array([0, 9, 0, 0, 0, 0, 0, 0]) df = pd.DataFrame({"key": key, "val": val}) df.groupby("key").idxmax()

Then I get the results I expect

Issue Description

It seems for a groupby where the maximum value for a group is 0, the idxmax aggregation is returning 0 instead of the index. I'm only seeing this when aggregating on a uint column.

Expected Behavior

I would expect the uint64 and int64 columns to give the same result

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.11.7.final.0
python-bits : 64
OS : Darwin
OS-release : 23.0.0
Version : Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : 7.4.4
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.20.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : 2.8.8
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : 3.9.2
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None