Skip to content

TEST: join df with categorical multiIndex #51088

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 3 commits
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
32 changes: 32 additions & 0 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,3 +956,35 @@ def test_join_empty(left_empty, how, exp):
expected = expected.rename_axis("A")

tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("how", ["inner", "outer", "left", "right"])
def test_join_multiindex_categorical_output_index_dtype(how):
# GH#50906
df1 = DataFrame(
{
"idx1": Categorical(["a", "a", "a"]),
"idx2": Categorical(["a", "a", "b"]),
"data": [1, 2, 3],
}
).set_index(["idx1", "idx2"])
Copy link
Member

Choose a reason for hiding this comment

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

thanks for working on this

why not use the example from #50906? for regression tests, if the original example is small and self-contained, it'd probably be safer to just use that directly
the rest looks good though 👍


df2 = DataFrame(
{
"idx1": Categorical(["a", "a", "a"]),
"idx2": Categorical(["a", "b", "b"]),
"data2": [1, 2, 3],
}
).set_index(["idx1", "idx2"])

expected = DataFrame(
{
"idx1": Categorical(["a", "a", "a", "a"]),
"idx2": Categorical(["a", "a", "b", "b"]),
"data": [1, 2, 3, 3],
"data2": [1, 1, 2, 3],
}
).set_index(["idx1", "idx2"])

result = df1.join(df2, how=how)
tm.assert_frame_equal(result, expected)