Skip to content

Commit f3cdd05

Browse files
authored
BUG: groupby.min with UInt64 and empty group (#46182)
1 parent 3960157 commit f3cdd05

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

pandas/_libs/groupby.pyx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,18 +1344,17 @@ cdef group_min_max(iu_64_floating_t[:, ::1] out,
13441344
for i in range(ngroups):
13451345
for j in range(K):
13461346
if nobs[i, j] < min_count:
1347-
if iu_64_floating_t is uint64_t:
1347+
if uses_mask:
1348+
result_mask[i, j] = True
1349+
# set out[i, j] to 0 to be deterministic, as
1350+
# it was initialized with np.empty. Also ensures
1351+
# we can downcast out if appropriate.
1352+
out[i, j] = 0
1353+
elif iu_64_floating_t is uint64_t:
13481354
runtime_error = True
13491355
break
13501356
else:
1351-
if uses_mask:
1352-
result_mask[i, j] = True
1353-
# set out[i, j] to 0 to be deterministic, as
1354-
# it was initialized with np.empty. Also ensures
1355-
# we can downcast out if appropriate.
1356-
out[i, j] = 0
1357-
else:
1358-
out[i, j] = nan_val
1357+
out[i, j] = nan_val
13591358
else:
13601359
out[i, j] = group_min_or_max[i, j]
13611360

pandas/tests/groupby/test_min_max.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,20 @@ def test_groupby_min_max_nullable(dtype):
225225

226226
res_max4 = gb2.max(min_count=100)
227227
tm.assert_frame_equal(res_max4, expected2)
228+
229+
230+
def test_min_max_nullable_uint64_empty_group():
231+
# don't raise NotImplementedError from libgroupby
232+
cat = pd.Categorical([0] * 10, categories=[0, 1])
233+
df = DataFrame({"A": cat, "B": pd.array(np.arange(10, dtype=np.uint64))})
234+
gb = df.groupby("A")
235+
236+
res = gb.min()
237+
238+
idx = pd.CategoricalIndex([0, 1], dtype=cat.dtype, name="A")
239+
expected = DataFrame({"B": pd.array([0, pd.NA], dtype="UInt64")}, index=idx)
240+
tm.assert_frame_equal(res, expected)
241+
242+
res = gb.max()
243+
expected.iloc[0, 0] = 9
244+
tm.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)