diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 6f361ff867c35..b72a21f1aa0c6 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -402,6 +402,8 @@ def _ea_wrap_cython_operation( if self.how in self.cast_blocklist: return res_values + elif self.how in ["first", "last", "min", "max"]: + res_values[result_mask == 1] = -1 return values._from_backing_data(res_values) npvalues = self._ea_to_cython_values(values) diff --git a/pandas/tests/groupby/test_min_max.py b/pandas/tests/groupby/test_min_max.py index 8602f8bdb1aa1..37eb52be0b37b 100644 --- a/pandas/tests/groupby/test_min_max.py +++ b/pandas/tests/groupby/test_min_max.py @@ -247,3 +247,26 @@ def test_min_max_nullable_uint64_empty_group(): res = gb.max() expected.iloc[0, 0] = 9 tm.assert_frame_equal(res, expected) + + +@pytest.mark.parametrize("func", ["first", "last", "min", "max"]) +def test_groupby_min_max_categorical(func): + # GH: 52151 + df = DataFrame( + { + "col1": pd.Categorical(["A"], categories=list("AB"), ordered=True), + "col2": pd.Categorical([1], categories=[1, 2], ordered=True), + "value": 0.1, + } + ) + result = getattr(df.groupby("col1", observed=False), func)() + + idx = pd.CategoricalIndex(data=["A", "B"], name="col1", ordered=True) + expected = DataFrame( + { + "col2": pd.Categorical([1, None], categories=[1, 2], ordered=True), + "value": [0.1, None], + }, + index=idx, + ) + tm.assert_frame_equal(result, expected)