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
27 changes: 27 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,33 @@ def func(dataf):
assert isinstance(result, DataFrame)


def test_multi_index_sort():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you rename to

test_sorting_with_different_categoricals

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to test_categorical.py (same directory)

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

df.dose = pd.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 = [
("A", "low"),
("A", "med"),
("A", "high"),
("B", "low"),
("B", "med"),
("B", "high"),
]
index = MultiIndex.from_tuples(index, names=["group", "dose"])
expected = Series([2] * 6, index=index, name="dose")
tm.assert_series_equal(result, expected)


def test_inconsistent_return_type():
# GH5592
# inconsistent return type
Expand Down