diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 0880b8e2cac12..46fbb41a86d2f 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -187,6 +187,7 @@ Removal of prior version deprecations/changes - Removed :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`) - Removed :attr:`Rolling.is_datetimelike` (:issue:`38963`) - Removed deprecated :meth:`Timedelta.delta`, :meth:`Timedelta.is_populated`, and :attr:`Timedelta.freq` (:issue:`46430`, :issue:`46476`) +- Removed deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`) - Removed the ``numeric_only`` keyword from :meth:`Categorical.min` and :meth:`Categorical.max` in favor of ``skipna`` (:issue:`48821`) - Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`) - Removed :meth:`Index.get_value` (:issue:`33907`) diff --git a/pandas/conftest.py b/pandas/conftest.py index 074b6068a4518..d9f481d843b37 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -154,7 +154,6 @@ def pytest_collection_modifyitems(items, config) -> None: ("DataFrame.append", "The frame.append method is deprecated"), ("Series.append", "The series.append method is deprecated"), ("dtypes.common.is_categorical", "is_categorical is deprecated"), - ("Categorical.replace", "Categorical.replace is deprecated"), ("MultiIndex._is_lexsorted", "MultiIndex.is_lexsorted is deprecated"), # Docstring divides by zero to show behavior difference ("missing.mask_zero_div_zero", "divide by zero encountered"), diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 48371b7f14b28..adeb53feaf5b6 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2561,53 +2561,6 @@ def isin(self, values) -> npt.NDArray[np.bool_]: code_values = code_values[null_mask | (code_values >= 0)] return algorithms.isin(self.codes, code_values) - @overload - def replace( - self, to_replace, value, *, inplace: Literal[False] = ... - ) -> Categorical: - ... - - @overload - def replace(self, to_replace, value, *, inplace: Literal[True]) -> None: - ... - - @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "value"]) - def replace(self, to_replace, value, inplace: bool = False) -> Categorical | None: - """ - Replaces all instances of one value with another - - Parameters - ---------- - to_replace: object - The value to be replaced - - value: object - The value to replace it with - - inplace: bool - Whether the operation is done in-place - - Returns - ------- - None if inplace is True, otherwise the new Categorical after replacement - - - Examples - -------- - >>> s = pd.Categorical([1, 2, 1, 3]) - >>> s.replace(1, 3) - [3, 2, 3, 3] - Categories (2, int64): [2, 3] - """ - # GH#44929 deprecation - warn( - "Categorical.replace is deprecated and will be removed in a future " - "version. Use Series.replace directly instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - return self._replace(to_replace=to_replace, value=value, inplace=inplace) - def _replace(self, *, to_replace, value, inplace: bool = False): inplace = validate_bool_kwarg(inplace, "inplace") cat = self if inplace else self.copy() diff --git a/pandas/tests/arrays/categorical/test_replace.py b/pandas/tests/arrays/categorical/test_replace.py index a50b1eddd99be..a3ba420c84a17 100644 --- a/pandas/tests/arrays/categorical/test_replace.py +++ b/pandas/tests/arrays/categorical/test_replace.py @@ -55,9 +55,7 @@ def test_replace_categorical(to_replace, value, result, expected_error_msg): # GH#26988 cat = Categorical(["a", "b"]) expected = Categorical(result) - with tm.assert_produces_warning(FutureWarning, match="Series.replace"): - # GH#44929 replace->_replace - result = cat.replace(to_replace, value) + result = pd.Series(cat).replace(to_replace, value)._values tm.assert_categorical_equal(result, expected) if to_replace == "b": # the "c" test is supposed to be unchanged @@ -65,8 +63,5 @@ def test_replace_categorical(to_replace, value, result, expected_error_msg): # ensure non-inplace call does not affect original tm.assert_categorical_equal(cat, expected) - with tm.assert_produces_warning(FutureWarning, match="Series.replace"): - # GH#44929 replace->_replace - cat.replace(to_replace, value, inplace=True) - + pd.Series(cat).replace(to_replace, value, inplace=True) tm.assert_categorical_equal(cat, expected)