From 0a3aa183fcd48f2af14209e4de8cf68e315f4141 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sun, 2 Jul 2023 20:37:23 +0200 Subject: [PATCH 1/2] TST: add test to check dtype after replacing categorical Series inplace --- pandas/tests/series/methods/test_replace.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index d3cdae63d26f3..510530ea23851 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -387,6 +387,16 @@ def test_replace_categorical(self, categorical, numeric): expected = expected.cat.add_categories(2) tm.assert_series_equal(expected, result) + @pytest.mark.parametrize( + "data, data_exp", [(["a", "b", "c"], ["b", "b", "c"]), (["a"], ["b"])] + ) + def test_replace_categorical_inplace(self, data, data_exp): + # GH 53358 + result = pd.Series(data, dtype="category") + result.replace(to_replace="a", value="b", inplace=True) + expected = pd.Series(data_exp, dtype="category") + tm.assert_index_equal(expected.dtype.categories, result.dtype.categories) + def test_replace_categorical_single(self): # GH 26988 dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific") From 454549c468f2714cdd16a5460572da2c9decb42d Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Fri, 7 Jul 2023 09:23:24 +0200 Subject: [PATCH 2/2] replace tm.assert_index_equal with tm.assert_series_equal --- pandas/tests/series/methods/test_replace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 510530ea23851..50b9714082054 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -395,7 +395,7 @@ def test_replace_categorical_inplace(self, data, data_exp): result = pd.Series(data, dtype="category") result.replace(to_replace="a", value="b", inplace=True) expected = pd.Series(data_exp, dtype="category") - tm.assert_index_equal(expected.dtype.categories, result.dtype.categories) + tm.assert_series_equal(result, expected) def test_replace_categorical_single(self): # GH 26988