Skip to content

Commit a19b981

Browse files
authored
DEPR: enforce deprecation of Categorical.replace (#49255)
1 parent edbac36 commit a19b981

File tree

4 files changed

+3
-55
lines changed

4 files changed

+3
-55
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Removal of prior version deprecations/changes
187187
- Removed :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`)
188188
- Removed :attr:`Rolling.is_datetimelike` (:issue:`38963`)
189189
- Removed deprecated :meth:`Timedelta.delta`, :meth:`Timedelta.is_populated`, and :attr:`Timedelta.freq` (:issue:`46430`, :issue:`46476`)
190+
- Removed deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
190191
- Removed the ``numeric_only`` keyword from :meth:`Categorical.min` and :meth:`Categorical.max` in favor of ``skipna`` (:issue:`48821`)
191192
- Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`)
192193
- Removed :meth:`Index.get_value` (:issue:`33907`)

pandas/conftest.py

-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ def pytest_collection_modifyitems(items, config) -> None:
154154
("DataFrame.append", "The frame.append method is deprecated"),
155155
("Series.append", "The series.append method is deprecated"),
156156
("dtypes.common.is_categorical", "is_categorical is deprecated"),
157-
("Categorical.replace", "Categorical.replace is deprecated"),
158157
("MultiIndex._is_lexsorted", "MultiIndex.is_lexsorted is deprecated"),
159158
# Docstring divides by zero to show behavior difference
160159
("missing.mask_zero_div_zero", "divide by zero encountered"),

pandas/core/arrays/categorical.py

-47
Original file line numberDiff line numberDiff line change
@@ -2561,53 +2561,6 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
25612561
code_values = code_values[null_mask | (code_values >= 0)]
25622562
return algorithms.isin(self.codes, code_values)
25632563

2564-
@overload
2565-
def replace(
2566-
self, to_replace, value, *, inplace: Literal[False] = ...
2567-
) -> Categorical:
2568-
...
2569-
2570-
@overload
2571-
def replace(self, to_replace, value, *, inplace: Literal[True]) -> None:
2572-
...
2573-
2574-
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "value"])
2575-
def replace(self, to_replace, value, inplace: bool = False) -> Categorical | None:
2576-
"""
2577-
Replaces all instances of one value with another
2578-
2579-
Parameters
2580-
----------
2581-
to_replace: object
2582-
The value to be replaced
2583-
2584-
value: object
2585-
The value to replace it with
2586-
2587-
inplace: bool
2588-
Whether the operation is done in-place
2589-
2590-
Returns
2591-
-------
2592-
None if inplace is True, otherwise the new Categorical after replacement
2593-
2594-
2595-
Examples
2596-
--------
2597-
>>> s = pd.Categorical([1, 2, 1, 3])
2598-
>>> s.replace(1, 3)
2599-
[3, 2, 3, 3]
2600-
Categories (2, int64): [2, 3]
2601-
"""
2602-
# GH#44929 deprecation
2603-
warn(
2604-
"Categorical.replace is deprecated and will be removed in a future "
2605-
"version. Use Series.replace directly instead.",
2606-
FutureWarning,
2607-
stacklevel=find_stack_level(),
2608-
)
2609-
return self._replace(to_replace=to_replace, value=value, inplace=inplace)
2610-
26112564
def _replace(self, *, to_replace, value, inplace: bool = False):
26122565
inplace = validate_bool_kwarg(inplace, "inplace")
26132566
cat = self if inplace else self.copy()

pandas/tests/arrays/categorical/test_replace.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,13 @@ def test_replace_categorical(to_replace, value, result, expected_error_msg):
5555
# GH#26988
5656
cat = Categorical(["a", "b"])
5757
expected = Categorical(result)
58-
with tm.assert_produces_warning(FutureWarning, match="Series.replace"):
59-
# GH#44929 replace->_replace
60-
result = cat.replace(to_replace, value)
58+
result = pd.Series(cat).replace(to_replace, value)._values
6159

6260
tm.assert_categorical_equal(result, expected)
6361
if to_replace == "b": # the "c" test is supposed to be unchanged
6462
with pytest.raises(AssertionError, match=expected_error_msg):
6563
# ensure non-inplace call does not affect original
6664
tm.assert_categorical_equal(cat, expected)
6765

68-
with tm.assert_produces_warning(FutureWarning, match="Series.replace"):
69-
# GH#44929 replace->_replace
70-
cat.replace(to_replace, value, inplace=True)
71-
66+
pd.Series(cat).replace(to_replace, value, inplace=True)
7267
tm.assert_categorical_equal(cat, expected)

0 commit comments

Comments
 (0)