Skip to content

TST: Test for MultiIndex merge with CategoricalIndex (#36973) #43433

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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2542,3 +2542,32 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
df_2 = DataFrame(data=["X"], columns=["C"], index=[999])
with pytest.raises(MergeError, match="Can only pass argument"):
merge(df_1, df_2, on=["C"], left_index=True)


def test_multiindex_merge_with_unordered_categoricalindex():
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 parametrized over ordered=True/False (test name can stay the same)

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 locate this test near other catorical merge tests

test_merge_categorical
tests_merge_categorical_unordered_equal # maybe a dupe of this one

if not a dupe, then ok to add this test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing. It doesn't look like the test is a duplicate

# GH 36973
pcat = CategoricalDtype(categories=["P2", "P1"], ordered=False)
df1 = DataFrame(
{
"id": ["C", "C", "D"],
"p": Categorical(["P2", "P1", "P2"], dtype=pcat),
"a": [0, 1, 2],
}
).set_index(["id", "p"])
df2 = DataFrame(
{
"id": ["A", "C", "C"],
"p": Categorical(["P2", "P2", "P1"], dtype=pcat),
"d1": [10, 11, 12],
}
).set_index(["id", "p"])
result = merge(df1, df2, how="left", left_index=True, right_index=True)
expected = DataFrame(
{
"id": ["C", "C", "D"],
"p": Categorical(["P2", "P1", "P2"], dtype=pcat),
"a": [0, 1, 2],
"d1": [11.0, 12.0, np.nan],
}
).set_index(["id", "p"])
tm.assert_frame_equal(result, expected)