Skip to content

add multi index with categories test #50697

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
merged 2 commits into from
Jan 12, 2023
Merged
Changes from all 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
37 changes: 37 additions & 0 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,43 @@ def test_intersection_lexsort_depth(levels1, levels2, codes1, codes2, names):
assert mi_int._lexsort_depth == 2


@pytest.mark.parametrize(
"a",
[pd.Categorical(["a", "b"], categories=["a", "b"]), ["a", "b"]],
)
@pytest.mark.parametrize(
"b",
[
pd.Categorical(["a", "b"], categories=["b", "a"], ordered=True),
pd.Categorical(["a", "b"], categories=["b", "a"]),
],
)
def test_intersection_with_non_lex_sorted_categories(a, b):
# GH#49974
other = ["1", "2"]

df1 = DataFrame({"x": a, "y": other})
df2 = DataFrame({"x": b, "y": other})

expected = MultiIndex.from_arrays([a, other], names=["x", "y"])

res1 = MultiIndex.from_frame(df1).intersection(
MultiIndex.from_frame(df2.sort_values(["x", "y"]))
)
res2 = MultiIndex.from_frame(df1).intersection(MultiIndex.from_frame(df2))
res3 = MultiIndex.from_frame(df1.sort_values(["x", "y"])).intersection(
MultiIndex.from_frame(df2)
)
res4 = MultiIndex.from_frame(df1.sort_values(["x", "y"])).intersection(
MultiIndex.from_frame(df2.sort_values(["x", "y"]))
)

tm.assert_index_equal(res1, expected)
tm.assert_index_equal(res2, expected)
tm.assert_index_equal(res3, expected)
tm.assert_index_equal(res4, expected)


@pytest.mark.parametrize("val", [pd.NA, 100])
def test_intersection_keep_ea_dtypes(val, any_numeric_ea_dtype):
# GH#48604
Expand Down