Skip to content

Commit

Permalink
BUG: groupby.agg returns incorrect results for uint64 cols (pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Heikkilä committed May 12, 2019
1 parent 17247ed commit 5edaa42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ def ensure_categorical(arr):
def ensure_int64_or_float64(arr, copy=False):
"""
Ensure that an dtype array of some integer dtype
has an int64 dtype if possible
has an int64 dtype if possible.
If it's not possible, potentially because of overflow,
convert the array to float64 instead.
If the array is explicitly of type uint64 the type
will remain unchanged.
Parameters
----------
Expand All @@ -107,11 +109,16 @@ def ensure_int64_or_float64(arr, copy=False):
out_arr : The input array cast as int64 if
possible without overflow.
Otherwise the input array cast to float64.
If the array is explicitly of type uint64 the type
will remain unchanged.
"""
try:
return arr.astype('int64', copy=copy, casting='safe')
except TypeError:
return arr.astype('float64', copy=copy)
try:
return arr.astype('uint64', copy=copy, casting='safe')
except TypeError:
return arr.astype('float64', copy=copy)


def classes(*klasses):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,14 @@ def test_order_aggregate_multiple_funcs():
expected = pd.Index(['sum', 'max', 'mean', 'ohlc', 'min'])

tm.assert_index_equal(result, expected)


def test_uint64_type_handling():
# GH 26310
df1 = pd.DataFrame({'x': 6903052872240755750, 'y': [1, 2]})
df1.groupby('y').agg({'x': 'first'})
df2 = df1
df2.x = df2.x.astype(np.uint64)
df2.groupby('y').agg({'x': 'first'})

tm.assert_frame_equal(df1, df2)

0 comments on commit 5edaa42

Please sign in to comment.