From 2dafa84eaecad1a15e73b944e57533a41fc16167 Mon Sep 17 00:00:00 2001 From: D067751 Date: Fri, 21 Apr 2023 01:10:55 +0200 Subject: [PATCH] Test for #34164 --- pandas/tests/groupby/test_groupby.py | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index b5b13d6b10511..41bd9454aa961 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -16,6 +16,7 @@ DataFrame, Grouper, Index, + Interval, MultiIndex, RangeIndex, Series, @@ -2970,3 +2971,44 @@ def test_groupby_numeric_only_std_no_result(numeric_only): ValueError, match="could not convert string to float: 'bar'" ): dfgb.std(numeric_only=numeric_only) + + +def test_grouping_with_categorical_interval_columns(): + # GH#34164 + df = DataFrame({"x": [0.1, 0.2, 0.3, -0.4, 0.5], "w": ["a", "b", "a", "c", "a"]}) + qq = pd.qcut(df["x"], q=np.linspace(0, 1, 5)) + result = df.groupby([qq, "w"], observed=False)["x"].agg("mean") + categorical_index_level_1 = Categorical( + [ + Interval(-0.401, 0.1, closed="right"), + Interval(0.1, 0.2, closed="right"), + Interval(0.2, 0.3, closed="right"), + Interval(0.3, 0.5, closed="right"), + ], + ordered=True, + ) + index_level_2 = ["a", "b", "c"] + mi = MultiIndex.from_product( + [categorical_index_level_1, index_level_2], names=["x", "w"] + ) + expected = Series( + np.array( + [ + 0.1, + np.nan, + -0.4, + np.nan, + 0.2, + np.nan, + 0.3, + np.nan, + np.nan, + 0.5, + np.nan, + np.nan, + ] + ), + index=mi, + name="x", + ) + tm.assert_series_equal(result, expected)