From ade42fd296a8fd5ea76c220f2f7b67dd2f428393 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 28 Feb 2022 10:14:08 -0800 Subject: [PATCH] BUG: groupby.min with UInt64 and empty group --- pandas/_libs/groupby.pyx | 9 ++++----- pandas/tests/groupby/test_min_max.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx index e9d6b6813c6fd..474d0b435fbb9 100644 --- a/pandas/_libs/groupby.pyx +++ b/pandas/_libs/groupby.pyx @@ -1344,14 +1344,13 @@ 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 + elif iu_64_floating_t is uint64_t: runtime_error = True break else: - if uses_mask: - result_mask[i, j] = True - else: - out[i, j] = nan_val + out[i, j] = nan_val else: out[i, j] = group_min_or_max[i, j] diff --git a/pandas/tests/groupby/test_min_max.py b/pandas/tests/groupby/test_min_max.py index 767ef2915d66e..b26ee057d2041 100644 --- a/pandas/tests/groupby/test_min_max.py +++ b/pandas/tests/groupby/test_min_max.py @@ -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)