Skip to content

Groupby multiple columns causes agg to have precision loss on int64 #33234

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
Lawrr opened this issue Apr 2, 2020 · 4 comments · Fixed by #51753
Closed

Groupby multiple columns causes agg to have precision loss on int64 #33234

Lawrr opened this issue Apr 2, 2020 · 4 comments · Fixed by #51753
Assignees
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions

Comments

@Lawrr
Copy link

Lawrr commented Apr 2, 2020

Code Sample, a copy-pastable example if possible

import pandas as pd

df=pd.DataFrame({
    "KEY1": ["KEY1"],
    "KEY2": ["KEY2"],
    "INT64": [1583715738627261039]
})

# Grouping by one column produces correct output
print(df.groupby(["KEY1"]).agg(lambda x: x))

# Grouping by more than one column produces incorrect output
print(df.groupby(["KEY1", "KEY2"]).agg(lambda x: x))

Problem description

Precision loss on the int64 column when grouping by multiple columns.

Similar behaviour can be seen when doing

>>> df.groupby(["KEY1"]).sum()
                    INT64
KEY1                     
KEY1  1583715738627260928

but that seems like a different issue with cythonized group sum not supporting int64? According to: #15027 (comment)

Expected Output

      KEY2                INT64
KEY1                           
KEY1  KEY2  1583715738627261039
                         INT64
KEY1 KEY2                     
KEY1 KEY2  1583715738627261039

Actual Output

      KEY2                INT64
KEY1                           
KEY1  KEY2  1583715738627261039
                         INT64
KEY1 KEY2                     
KEY1 KEY2  1583715738627260928

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.5.final.0
python-bits : 64
OS : Linux
OS-release : 3.16.0-77-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8

pandas : 1.0.3
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3
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
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@simonjayhawkins simonjayhawkins added Bug Dtype Conversions Unexpected or buggy dtype conversions Groupby labels Apr 2, 2020
@simonjayhawkins
Copy link
Member

This also applies to nullable integers

>>> import pandas as pd
>>>
>>> pd.__version__
'1.1.0.dev0+1068.g49bc8d8c9'
>>>
>>> df = pd.DataFrame(
...     {
...         "KEY1": ["KEY1"],
...         "KEY2": ["KEY2"],
...         "INT64": pd.array([1583715738627261039], dtype="Int64"),
...     }
... )
>>> df
   KEY1  KEY2                INT64
0  KEY1  KEY2  1583715738627261039
>>>
>>> # Grouping by one column produces correct output
>>> print(df.groupby(["KEY1"]).agg(lambda x: x))
      KEY2                INT64
KEY1
KEY1  KEY2  1583715738627261039
>>>
>>> df.groupby(["KEY1"]).agg(lambda x: x).dtypes
KEY2     object
INT64     Int64
dtype: object
>>>
>>> # Grouping by more than one column produces incorrect output
>>> print(df.groupby(["KEY1", "KEY2"]).agg(lambda x: x))
                  INT64
KEY1 KEY2
KEY1 KEY2  1.583716e+18
>>>
>>> df.groupby(["KEY1", "KEY2"]).agg(lambda x: x).dtypes
INT64    float64
dtype: object
>>>

@mroeschke
Copy link
Member

This looks okay on master. Could use a test

In [7]: import pandas as pd
   ...:
   ...: df=pd.DataFrame({
   ...:     "KEY1": ["KEY1"],
   ...:     "KEY2": ["KEY2"],
   ...:     "INT64": [1583715738627261039]
   ...: })
   ...:
   ...: # Grouping by one column produces correct output
   ...: print(df.groupby(["KEY1"]).agg(lambda x: x))
   ...:
   ...: # Grouping by more than one column produces incorrect output
   ...: print(df.groupby(["KEY1", "KEY2"]).agg(lambda x: x))
      KEY2                INT64
KEY1
KEY1  KEY2  1583715738627261039
                         INT64
KEY1 KEY2
KEY1 KEY2  1583715738627261039

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Jul 31, 2021
@whitneymichelle
Copy link

take

@simonjayhawkins
Copy link
Member

removing milestone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions good first issue Groupby Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
5 participants