Skip to content
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

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

Closed
1 task
ppletscher opened this issue May 31, 2021 · 3 comments · Fixed by #42567
Closed
1 task

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

ppletscher opened this issue May 31, 2021 · 3 comments · Fixed by #42567
Assignees
Labels
Bug Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Reduction Operations sum, mean, min, max, etc.
Milestone

Comments

@ppletscher
Copy link

  • [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:

@ppletscher ppletscher added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 31, 2021
@jreback
Copy link
Contributor

jreback commented May 31, 2021

try this in master as pretty sure this is fixed

@ppletscher
Copy link
Author

Thanks a lot for the quick reply.

I've just tried this inside a Docker build as described here and unfortunately I also see the same behaviour there.

pd.show_versions() shows the following:

INSTALLED VERSIONS
------------------
commit           : bc9b470711cd2126d0a834186103a4ea2215f7eb
python           : 3.8.10.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           : C.UTF-8
LANG             : C.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.3.0.dev0+1766.gbc9b470711
numpy            : 1.20.3
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 21.1.2
setuptools       : 49.6.0.post20210108
Cython           : 0.29.23
pytest           : 6.2.4
hypothesis       : 6.13.10
sphinx           : 3.5.4
blosc            : None
feather          : None
xlsxwriter       : 1.4.3
lxml.etree       : 4.6.3
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : 2.11.3
IPython          : 7.24.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : 2021.05.0
fastparquet      : 0.6.3
gcsfs            : 2021.05.0
matplotlib       : 3.4.2
numexpr          : 2.7.3
odfpy            : None
openpyxl         : 3.0.7
pandas_gbq       : None
pyarrow          : 4.0.0
pyxlsb           : None
s3fs             : 0.4.2
scipy            : 1.6.3
sqlalchemy       : 1.4.17
tables           : 3.6.1
tabulate         : 0.8.9
xarray           : 0.18.2
xlrd             : 2.0.1
xlwt             : 1.3.0
numba            : 0.53.1

@mzeitlin11
Copy link
Member

mzeitlin11 commented May 31, 2021

Thanks for trying this in master! This cast was avoided for cummin/max in #40651, still needs to be done for min/max, xref #37493

@mzeitlin11 mzeitlin11 added Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Reduction Operations sum, mean, min, max, etc. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 31, 2021
@mzeitlin11 mzeitlin11 self-assigned this Jun 8, 2021
@jreback jreback added this to the 1.4 milestone Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays Reduction Operations sum, mean, min, max, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants