Skip to content

Commit 34c95ea

Browse files
committed
TST: Test for MultiIndex merge with CategoricalIndex (pandas-dev#36973)
1 parent 11a8c17 commit 34c95ea

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pandas/tests/reshape/merge/test_merge.py

+29
Original file line numberDiff line numberDiff line change
@@ -2542,3 +2542,32 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
25422542
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
25432543
with pytest.raises(MergeError, match="Can only pass argument"):
25442544
merge(df_1, df_2, on=["C"], left_index=True)
2545+
2546+
2547+
def test_multiindex_merge_with_unordered_categoricalindex():
2548+
# GH 36973
2549+
pcat = CategoricalDtype(categories=["P2", "P1"], ordered=False)
2550+
df1 = DataFrame(
2551+
{
2552+
"id": ["C", "C", "D"],
2553+
"p": Categorical(["P2", "P1", "P2"], dtype=pcat),
2554+
"a": [0, 1, 2],
2555+
}
2556+
).set_index(["id", "p"])
2557+
df2 = DataFrame(
2558+
{
2559+
"id": ["A", "C", "C"],
2560+
"p": Categorical(["P2", "P2", "P1"], dtype=pcat),
2561+
"d1": [10, 11, 12],
2562+
}
2563+
).set_index(["id", "p"])
2564+
result = merge(df1, df2, how="left", left_index=True, right_index=True)
2565+
expected = DataFrame(
2566+
{
2567+
"id": ["C", "C", "D"],
2568+
"p": Categorical(["P2", "P1", "P2"], dtype=pcat),
2569+
"a": [0, 1, 2],
2570+
"d1": [11.0, 12.0, np.nan],
2571+
}
2572+
).set_index(["id", "p"])
2573+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)