Skip to content

BUG: groupby().agg( ) with min/max on Int64 leads to incorrect results #41743

Closed
@ppletscher

Description

@ppletscher
  • [x ] I have checked that this issue has not already been reported.

  • [x ] 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.


Code Sample, a copy-pastable example

import pandas as pd

ts = 1618556707013635762
pdf = pd.DataFrame([{"id": 2, "ts": ts}, {"id": 2, "ts": ts+1}])
pdf["ts"] = pd.array(pdf["ts"], dtype="Int64")
pdf.groupby("id").agg({"ts": "min"})

Problem description

The above code will output 1618556707013635840 as the minimum which is incorrect.

Expected Output

Minimum should be 1618556707013635762. I suspect there is a cast happening somewhere during the aggregation. If we keep the ts column at int64 (i.e. use the non-nullable type) the result is as expected.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.7-200.fc33.x86_64
Version : #1 SMP Wed Mar 17 18:55:20 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL :
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.1
setuptools : 56.0.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.24.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Potentially related issues

The issues below seem related, but from what I can see are slightly different:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions