Skip to content

Commit

Permalink
BUG: groupby.min with UInt64 and empty group (pandas-dev#46182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Mar 1, 2022
1 parent 3960157 commit f3cdd05
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1344,18 +1344,17 @@ cdef group_min_max(iu_64_floating_t[:, ::1] out,
for i in range(ngroups):
for j in range(K):
if nobs[i, j] < min_count:
if iu_64_floating_t is uint64_t:
if uses_mask:
result_mask[i, j] = True
# set out[i, j] to 0 to be deterministic, as
# it was initialized with np.empty. Also ensures
# we can downcast out if appropriate.
out[i, j] = 0
elif iu_64_floating_t is uint64_t:
runtime_error = True
break
else:
if uses_mask:
result_mask[i, j] = True
# set out[i, j] to 0 to be deterministic, as
# it was initialized with np.empty. Also ensures
# we can downcast out if appropriate.
out[i, j] = 0
else:
out[i, j] = nan_val
out[i, j] = nan_val
else:
out[i, j] = group_min_or_max[i, j]

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/groupby/test_min_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,20 @@ def test_groupby_min_max_nullable(dtype):

res_max4 = gb2.max(min_count=100)
tm.assert_frame_equal(res_max4, expected2)


def test_min_max_nullable_uint64_empty_group():
# don't raise NotImplementedError from libgroupby
cat = pd.Categorical([0] * 10, categories=[0, 1])
df = DataFrame({"A": cat, "B": pd.array(np.arange(10, dtype=np.uint64))})
gb = df.groupby("A")

res = gb.min()

idx = pd.CategoricalIndex([0, 1], dtype=cat.dtype, name="A")
expected = DataFrame({"B": pd.array([0, pd.NA], dtype="UInt64")}, index=idx)
tm.assert_frame_equal(res, expected)

res = gb.max()
expected.iloc[0, 0] = 9
tm.assert_frame_equal(res, expected)

0 comments on commit f3cdd05

Please sign in to comment.