Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TST] BUG: grouping with categorical interval columns #52818

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DataFrame,
Grouper,
Index,
Interval,
MultiIndex,
RangeIndex,
Series,
Expand Down Expand Up @@ -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)