Skip to content

Commit 60e9189

Browse files
authored
sort_index not sorting when multi-index made by different categorical types (#39986)
1 parent 1ced878 commit 60e9189

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pandas/tests/groupby/test_categorical.py

+22
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ def test_level_get_group(observed):
241241
tm.assert_frame_equal(result, expected)
242242

243243

244+
def test_sorting_with_different_categoricals():
245+
# GH 24271
246+
df = DataFrame(
247+
{
248+
"group": ["A"] * 6 + ["B"] * 6,
249+
"dose": ["high", "med", "low"] * 4,
250+
"outcomes": np.arange(12.0),
251+
}
252+
)
253+
254+
df.dose = Categorical(df.dose, categories=["low", "med", "high"], ordered=True)
255+
256+
result = df.groupby("group")["dose"].value_counts()
257+
result = result.sort_index(level=0, sort_remaining=True)
258+
index = ["low", "med", "high", "low", "med", "high"]
259+
index = Categorical(index, categories=["low", "med", "high"], ordered=True)
260+
index = [["A", "A", "A", "B", "B", "B"], CategoricalIndex(index)]
261+
index = MultiIndex.from_arrays(index, names=["group", None])
262+
expected = Series([2] * 6, index=index, name="dose")
263+
tm.assert_series_equal(result, expected)
264+
265+
244266
@pytest.mark.parametrize("ordered", [True, False])
245267
def test_apply(ordered):
246268
# GH 10138

0 commit comments

Comments
 (0)