Skip to content

sort_index not sorting when multi-index made by different categorical types #39986

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

Merged
merged 12 commits into from
Mar 15, 2021
22 changes: 22 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ def test_level_get_group(observed):
tm.assert_frame_equal(result, expected)


def test_sorting_with_different_categoricals():
# GH 24271
df = DataFrame(
{
"group": ["A"] * 6 + ["B"] * 6,
"dose": ["high", "med", "low"] * 4,
"outcomes": np.arange(12.0),
}
)

df.dose = Categorical(df.dose, categories=["low", "med", "high"], ordered=True)

result = df.groupby("group")["dose"].value_counts()
result = result.sort_index(level=0, sort_remaining=True)
index = ["low", "med", "high", "low", "med", "high"]
index = Categorical(index, categories=["low", "med", "high"], ordered=True)
index = [["A", "A", "A", "B", "B", "B"], CategoricalIndex(index)]
index = MultiIndex.from_arrays(index, names=["group", None])
expected = Series([2] * 6, index=index, name="dose")
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("ordered", [True, False])
def test_apply(ordered):
# GH 10138
Expand Down