@@ -692,6 +692,43 @@ def test_intersection_lexsort_depth(levels1, levels2, codes1, codes2, names):
692
692
assert mi_int ._lexsort_depth == 2
693
693
694
694
695
+ @pytest .mark .parametrize (
696
+ "a" ,
697
+ [pd .Categorical (["a" , "b" ], categories = ["a" , "b" ]), ["a" , "b" ]],
698
+ )
699
+ @pytest .mark .parametrize (
700
+ "b" ,
701
+ [
702
+ pd .Categorical (["a" , "b" ], categories = ["b" , "a" ], ordered = True ),
703
+ pd .Categorical (["a" , "b" ], categories = ["b" , "a" ]),
704
+ ],
705
+ )
706
+ def test_intersection_with_non_lex_sorted_categories (a , b ):
707
+ # GH#49974
708
+ other = ["1" , "2" ]
709
+
710
+ df1 = DataFrame ({"x" : a , "y" : other })
711
+ df2 = DataFrame ({"x" : b , "y" : other })
712
+
713
+ expected = MultiIndex .from_arrays ([a , other ], names = ["x" , "y" ])
714
+
715
+ res1 = MultiIndex .from_frame (df1 ).intersection (
716
+ MultiIndex .from_frame (df2 .sort_values (["x" , "y" ]))
717
+ )
718
+ res2 = MultiIndex .from_frame (df1 ).intersection (MultiIndex .from_frame (df2 ))
719
+ res3 = MultiIndex .from_frame (df1 .sort_values (["x" , "y" ])).intersection (
720
+ MultiIndex .from_frame (df2 )
721
+ )
722
+ res4 = MultiIndex .from_frame (df1 .sort_values (["x" , "y" ])).intersection (
723
+ MultiIndex .from_frame (df2 .sort_values (["x" , "y" ]))
724
+ )
725
+
726
+ tm .assert_index_equal (res1 , expected )
727
+ tm .assert_index_equal (res2 , expected )
728
+ tm .assert_index_equal (res3 , expected )
729
+ tm .assert_index_equal (res4 , expected )
730
+
731
+
695
732
@pytest .mark .parametrize ("val" , [pd .NA , 100 ])
696
733
def test_intersection_keep_ea_dtypes (val , any_numeric_ea_dtype ):
697
734
# GH#48604
0 commit comments