@@ -2542,3 +2542,32 @@ def test_mergeerror_on_left_index_mismatched_dtypes():
2542
2542
df_2 = DataFrame (data = ["X" ], columns = ["C" ], index = [999 ])
2543
2543
with pytest .raises (MergeError , match = "Can only pass argument" ):
2544
2544
merge (df_1 , df_2 , on = ["C" ], left_index = True )
2545
+
2546
+
2547
+ def test_multiindex_merge_with_unordered_categoricalindex ():
2548
+ # GH 36973
2549
+ pcat = CategoricalDtype (categories = ["P2" , "P1" ], ordered = False )
2550
+ df1 = DataFrame (
2551
+ {
2552
+ "id" : ["C" , "C" , "D" ],
2553
+ "p" : Categorical (["P2" , "P1" , "P2" ], dtype = pcat ),
2554
+ "a" : [0 , 1 , 2 ],
2555
+ }
2556
+ ).set_index (["id" , "p" ])
2557
+ df2 = DataFrame (
2558
+ {
2559
+ "id" : ["A" , "C" , "C" ],
2560
+ "p" : Categorical (["P2" , "P2" , "P1" ], dtype = pcat ),
2561
+ "d1" : [10 , 11 , 12 ],
2562
+ }
2563
+ ).set_index (["id" , "p" ])
2564
+ result = merge (df1 , df2 , how = "left" , left_index = True , right_index = True )
2565
+ expected = DataFrame (
2566
+ {
2567
+ "id" : ["C" , "C" , "D" ],
2568
+ "p" : Categorical (["P2" , "P1" , "P2" ], dtype = pcat ),
2569
+ "a" : [0 , 1 , 2 ],
2570
+ "d1" : [11.0 , 12.0 , np .nan ],
2571
+ }
2572
+ ).set_index (["id" , "p" ])
2573
+ tm .assert_frame_equal (result , expected )
0 commit comments