@@ -1296,6 +1296,24 @@ def test_replace_method(self, to_replace, method, expected):
12961296 expected = DataFrame (expected )
12971297 tm .assert_frame_equal (result , expected )
12981298
1299+ @pytest .mark .parametrize (
1300+ "replace_dict, final_data" ,
1301+ [({"a" : 1 , "b" : 1 }, [[3 , 3 ], [2 , 2 ]]), ({"a" : 1 , "b" : 2 }, [[3 , 1 ], [2 , 3 ]])],
1302+ )
1303+ def test_categorical_replace_with_dict (self , replace_dict , final_data ):
1304+ # GH 26988
1305+ df = DataFrame ([[1 , 1 ], [2 , 2 ]], columns = ["a" , "b" ], dtype = "category" )
1306+ expected = DataFrame (final_data , columns = ["a" , "b" ], dtype = "category" )
1307+ expected ["a" ] = expected ["a" ].cat .set_categories ([1 , 2 , 3 ])
1308+ expected ["b" ] = expected ["b" ].cat .set_categories ([1 , 2 , 3 ])
1309+ result = df .replace (replace_dict , 3 )
1310+ tm .assert_frame_equal (result , expected )
1311+ with pytest .raises (AssertionError ):
1312+ # ensure non-inplace call does not affect original
1313+ tm .assert_frame_equal (df , expected )
1314+ df .replace (replace_dict , 3 , inplace = True )
1315+ tm .assert_frame_equal (df , expected )
1316+
12991317 @pytest .mark .parametrize (
13001318 "df, to_replace, exp" ,
13011319 [
0 commit comments