From f113cd50f70f25be4a57173fbf7b1e38fda47aba Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 19 Aug 2022 23:20:48 +0200 Subject: [PATCH] TST: Add test for replace categorical no replacement changes dtype --- pandas/tests/frame/methods/test_replace.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index 555d24e747f44..177f3ec1b4504 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -1559,3 +1559,17 @@ def test_replace_with_value_also_being_replaced(self): result = df.replace({0: 1, 1: np.nan}) expected = DataFrame({"A": [1, np.nan, 2], "B": [np.nan, 1, 2]}) tm.assert_frame_equal(result, expected) + + def test_replace_categorical_no_replacement(self): + # GH#46672 + df = DataFrame( + { + "a": ["one", "two", None, "three"], + "b": ["one", None, "two", "three"], + }, + dtype="category", + ) + expected = df.copy() + + result = df.replace(to_replace=[".", "def"], value=["_", None]) + tm.assert_frame_equal(result, expected)