@@ -1813,6 +1813,35 @@ def tests_merge_categorical_unordered_equal(self):
1813
1813
)
1814
1814
tm .assert_frame_equal (result , expected )
1815
1815
1816
+ @pytest .mark .parametrize ("ordered" , [True , False ])
1817
+ def test_multiindex_merge_with_unordered_categoricalindex (self , ordered ):
1818
+ # GH 36973
1819
+ pcat = CategoricalDtype (categories = ["P2" , "P1" ], ordered = ordered )
1820
+ df1 = DataFrame (
1821
+ {
1822
+ "id" : ["C" , "C" , "D" ],
1823
+ "p" : Categorical (["P2" , "P1" , "P2" ], dtype = pcat ),
1824
+ "a" : [0 , 1 , 2 ],
1825
+ }
1826
+ ).set_index (["id" , "p" ])
1827
+ df2 = DataFrame (
1828
+ {
1829
+ "id" : ["A" , "C" , "C" ],
1830
+ "p" : Categorical (["P2" , "P2" , "P1" ], dtype = pcat ),
1831
+ "d1" : [10 , 11 , 12 ],
1832
+ }
1833
+ ).set_index (["id" , "p" ])
1834
+ result = merge (df1 , df2 , how = "left" , left_index = True , right_index = True )
1835
+ expected = DataFrame (
1836
+ {
1837
+ "id" : ["C" , "C" , "D" ],
1838
+ "p" : Categorical (["P2" , "P1" , "P2" ], dtype = pcat ),
1839
+ "a" : [0 , 1 , 2 ],
1840
+ "d1" : [11.0 , 12.0 , np .nan ],
1841
+ }
1842
+ ).set_index (["id" , "p" ])
1843
+ tm .assert_frame_equal (result , expected )
1844
+
1816
1845
def test_other_columns (self , left , right ):
1817
1846
# non-merge columns should preserve if possible
1818
1847
right = right .assign (Z = right .Z .astype ("category" ))
0 commit comments