Skip to content

Commit

Permalink
TST: Grouping with categorical interval columns (#52818)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimeF committed Apr 23, 2023
1 parent fc434c4 commit 3ec67e6
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Expand Up @@ -16,6 +16,7 @@
DataFrame,
Grouper,
Index,
Interval,
MultiIndex,
RangeIndex,
Series,
Expand Down Expand Up @@ -2972,6 +2973,47 @@ def test_groupby_numeric_only_std_no_result(numeric_only):
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)


@pytest.mark.parametrize("bug_var", [1, "a"])
def test_groupby_sum_on_nan_should_return_nan(bug_var):
# GH 24196
Expand Down

0 comments on commit 3ec67e6

Please sign in to comment.