Skip to content

Commit 7556d29

Browse files
authored
TST: Test for MultiIndex merge with CategoricalIndex (#36973) (#43433)
1 parent 73c473d commit 7556d29

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
@@ -1813,6 +1813,35 @@ def tests_merge_categorical_unordered_equal(self):
18131813
)
18141814
tm.assert_frame_equal(result, expected)
18151815

1816+
@pytest.mark.parametrize("ordered", [True, False])
1817+
def test_multiindex_merge_with_unordered_categoricalindex(self, ordered):
1818+
# GH 36973
1819+
pcat = CategoricalDtype(categories=["P2", "P1"], ordered=ordered)
1820+
df1 = DataFrame(
1821+
{
1822+
"id": ["C", "C", "D"],
1823+
"p": Categorical(["P2", "P1", "P2"], dtype=pcat),
1824+
"a": [0, 1, 2],
1825+
}
1826+
).set_index(["id", "p"])
1827+
df2 = DataFrame(
1828+
{
1829+
"id": ["A", "C", "C"],
1830+
"p": Categorical(["P2", "P2", "P1"], dtype=pcat),
1831+
"d1": [10, 11, 12],
1832+
}
1833+
).set_index(["id", "p"])
1834+
result = merge(df1, df2, how="left", left_index=True, right_index=True)
1835+
expected = DataFrame(
1836+
{
1837+
"id": ["C", "C", "D"],
1838+
"p": Categorical(["P2", "P1", "P2"], dtype=pcat),
1839+
"a": [0, 1, 2],
1840+
"d1": [11.0, 12.0, np.nan],
1841+
}
1842+
).set_index(["id", "p"])
1843+
tm.assert_frame_equal(result, expected)
1844+
18161845
def test_other_columns(self, left, right):
18171846
# non-merge columns should preserve if possible
18181847
right = right.assign(Z=right.Z.astype("category"))

0 commit comments

Comments
 (0)