From 876b4bcc289f2c375533eef14abd3fb28ee021f6 Mon Sep 17 00:00:00 2001 From: Hans Pagh Date: Wed, 30 Nov 2022 17:59:34 +0100 Subject: [PATCH 1/2] add multi index with categories test --- pandas/tests/indexes/multi/test_setops.py | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index d0345861d6778..aa6e472a7f5a5 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -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"]), + 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 From 78863988a5dcab9efc5e2df693a7c2a593a8dad6 Mon Sep 17 00:00:00 2001 From: Hans Pagh Date: Thu, 5 Jan 2023 10:41:06 +0100 Subject: [PATCH 2/2] add ordered param based on comments --- pandas/tests/indexes/multi/test_setops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/multi/test_setops.py b/pandas/tests/indexes/multi/test_setops.py index aa6e472a7f5a5..08ccf696d73f3 100644 --- a/pandas/tests/indexes/multi/test_setops.py +++ b/pandas/tests/indexes/multi/test_setops.py @@ -699,7 +699,7 @@ def test_intersection_lexsort_depth(levels1, levels2, codes1, codes2, names): @pytest.mark.parametrize( "b", [ - pd.Categorical(["a", "b"], categories=["b", "a"]), + pd.Categorical(["a", "b"], categories=["b", "a"], ordered=True), pd.Categorical(["a", "b"], categories=["b", "a"]), ], )