From 589037d4ac1e5b8f84e044dc683e2842e0466e47 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Mon, 27 Mar 2023 12:42:46 +0200 Subject: [PATCH 1/3] removed bug WrappedCythonOp --- pandas/core/groupby/ops.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index dad188e2d9304..255c9992920c4 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) From ec1dcc61ab19a3ee75d832e09530a2a9b9eb7bca Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Tue, 28 Mar 2023 19:08:53 +0200 Subject: [PATCH 2/3] added test --- pandas/tests/groupby/test_min_max.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pandas/tests/groupby/test_min_max.py b/pandas/tests/groupby/test_min_max.py index 8602f8bdb1aa1..21ef13171e993 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", ["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) From b8ed23e1e7725325eff7af82c61c130590381755 Mon Sep 17 00:00:00 2001 From: Dea Leon Date: Tue, 28 Mar 2023 19:16:41 +0200 Subject: [PATCH 3/3] added functions to test --- pandas/tests/groupby/test_min_max.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_min_max.py b/pandas/tests/groupby/test_min_max.py index 21ef13171e993..37eb52be0b37b 100644 --- a/pandas/tests/groupby/test_min_max.py +++ b/pandas/tests/groupby/test_min_max.py @@ -249,7 +249,7 @@ def test_min_max_nullable_uint64_empty_group(): tm.assert_frame_equal(res, expected) -@pytest.mark.parametrize("func", ["min", "max"]) +@pytest.mark.parametrize("func", ["first", "last", "min", "max"]) def test_groupby_min_max_categorical(func): # GH: 52151 df = DataFrame(