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

groupby producing incorrect results applying functions to int64 columns due to internal cast to float #6620

jbezaire opened this issue Mar 12, 2014 · 1 comment


None yet
2 participants
Copy link

commented Mar 12, 2014

dupe of #3707

groupby often casts int64 data to floats before applying functions to the grouped data, and then casts the result back to an int64. This produces incorrect results.
Here is a simple example:

import pandas as pd
import numpy as np
label = np.array([1, 2, 2, 3],
data = np.array([1, 2, 3,4], dtype=np.int64) + 24650000000000000
z = pd.DataFrame({'a':label,'data':data})
f = z.groupby('a').first()

In [40]: print z
   a               data
0  1  24650000000000001
1  2  24650000000000002
2  2  24650000000000003
3  3  24650000000000004

In [41]: print f
1  24650000000000000
2  24650000000000000
3  24650000000000004

The result is clearly incorrect and should be

1  24650000000000001
2  24650000000000002
3  24650000000000004 and are both numpy.int64, which masks the fact that was converted to a float with a loss of precision during the groupby operation.
Tests of software using groupby on int64 will only show a problem if the tests include integers large enough to cause this loss of precision. Otherwise the tests will pass and the software will just quietly produce incorrect results in production.


This comment has been minimized.

Copy link

commented Mar 12, 2014

this is a dupe of #3707. Welcome tests and a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.