Skip to content

Commit 3ec67e6

Browse files
authored
TST: Grouping with categorical interval columns (#52818)
1 parent fc434c4 commit 3ec67e6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pandas/tests/groupby/test_groupby.py

+42
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
DataFrame,
1717
Grouper,
1818
Index,
19+
Interval,
1920
MultiIndex,
2021
RangeIndex,
2122
Series,
@@ -2972,6 +2973,47 @@ def test_groupby_numeric_only_std_no_result(numeric_only):
29722973
dfgb.std(numeric_only=numeric_only)
29732974

29742975

2976+
def test_grouping_with_categorical_interval_columns():
2977+
# GH#34164
2978+
df = DataFrame({"x": [0.1, 0.2, 0.3, -0.4, 0.5], "w": ["a", "b", "a", "c", "a"]})
2979+
qq = pd.qcut(df["x"], q=np.linspace(0, 1, 5))
2980+
result = df.groupby([qq, "w"], observed=False)["x"].agg("mean")
2981+
categorical_index_level_1 = Categorical(
2982+
[
2983+
Interval(-0.401, 0.1, closed="right"),
2984+
Interval(0.1, 0.2, closed="right"),
2985+
Interval(0.2, 0.3, closed="right"),
2986+
Interval(0.3, 0.5, closed="right"),
2987+
],
2988+
ordered=True,
2989+
)
2990+
index_level_2 = ["a", "b", "c"]
2991+
mi = MultiIndex.from_product(
2992+
[categorical_index_level_1, index_level_2], names=["x", "w"]
2993+
)
2994+
expected = Series(
2995+
np.array(
2996+
[
2997+
0.1,
2998+
np.nan,
2999+
-0.4,
3000+
np.nan,
3001+
0.2,
3002+
np.nan,
3003+
0.3,
3004+
np.nan,
3005+
np.nan,
3006+
0.5,
3007+
np.nan,
3008+
np.nan,
3009+
]
3010+
),
3011+
index=mi,
3012+
name="x",
3013+
)
3014+
tm.assert_series_equal(result, expected)
3015+
3016+
29753017
@pytest.mark.parametrize("bug_var", [1, "a"])
29763018
def test_groupby_sum_on_nan_should_return_nan(bug_var):
29773019
# GH 24196

0 commit comments

Comments
 (0)