Skip to content

Commit 01268aa

Browse files
authored
DEPR: Categorical.replace (#44929)
1 parent 862c284 commit 01268aa

File tree

7 files changed

+29
-9
lines changed

7 files changed

+29
-9
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ Other Deprecations
538538
- Deprecated passing ``skipna=None`` for :meth:`DataFrame.mad` and :meth:`Series.mad`, pass ``skipna=True`` instead (:issue:`44580`)
539539
- Deprecated :meth:`DateOffset.apply`, use ``offset + other`` instead (:issue:`44522`)
540540
- A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`)
541+
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
541542
-
542543

543544
.. ---------------------------------------------------------------------------

pandas/core/arrays/categorical.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,6 +2457,16 @@ def replace(self, to_replace, value, inplace: bool = False):
24572457
[3, 2, 3, 3]
24582458
Categories (2, int64): [2, 3]
24592459
"""
2460+
# GH#44929 deprecation
2461+
warn(
2462+
"Categorical.replace is deprecated and will be removed in a future "
2463+
"version. Use Series.replace directly instead.",
2464+
FutureWarning,
2465+
stacklevel=find_stack_level(),
2466+
)
2467+
return self._replace(to_replace=to_replace, value=value, inplace=inplace)
2468+
2469+
def _replace(self, *, to_replace, value, inplace: bool = False):
24602470
inplace = validate_bool_kwarg(inplace, "inplace")
24612471
cat = self if inplace else self.copy()
24622472

pandas/core/internals/array_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def replace_list(
430430
inplace = validate_bool_kwarg(inplace, "inplace")
431431

432432
return self.apply_with_block(
433-
"_replace_list",
433+
"replace_list",
434434
src_list=src_list,
435435
dest_list=dest_list,
436436
inplace=inplace,

pandas/core/internals/blocks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,14 @@ def replace(
651651

652652
# Note: the checks we do in NDFrame.replace ensure we never get
653653
# here with listlike to_replace or value, as those cases
654-
# go through _replace_list
654+
# go through replace_list
655655

656656
values = self.values
657657

658658
if isinstance(values, Categorical):
659659
# TODO: avoid special-casing
660660
blk = self if inplace else self.copy()
661-
blk.values.replace(to_replace, value, inplace=True)
661+
blk.values._replace(to_replace=to_replace, value=value, inplace=True)
662662
return [blk]
663663

664664
regex = should_use_regex(regex, to_replace)
@@ -743,15 +743,15 @@ def _replace_regex(
743743
return [block]
744744

745745
@final
746-
def _replace_list(
746+
def replace_list(
747747
self,
748748
src_list: Iterable[Any],
749749
dest_list: Sequence[Any],
750750
inplace: bool = False,
751751
regex: bool = False,
752752
) -> list[Block]:
753753
"""
754-
See BlockManager._replace_list docstring.
754+
See BlockManager.replace_list docstring.
755755
"""
756756
values = self.values
757757

pandas/core/internals/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def replace_list(
449449
inplace = validate_bool_kwarg(inplace, "inplace")
450450

451451
bm = self.apply(
452-
"_replace_list",
452+
"replace_list",
453453
src_list=src_list,
454454
dest_list=dest_list,
455455
inplace=inplace,

pandas/tests/arrays/categorical/test_replace.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,18 @@ def test_replace_categorical(to_replace, value, result, expected_error_msg):
6363
# GH#26988
6464
cat = Categorical(["a", "b"])
6565
expected = Categorical(result)
66-
result = cat.replace(to_replace, value)
66+
with tm.assert_produces_warning(FutureWarning, match="Series.replace"):
67+
# GH#44929 replace->_replace
68+
result = cat.replace(to_replace, value)
69+
6770
tm.assert_categorical_equal(result, expected)
6871
if to_replace == "b": # the "c" test is supposed to be unchanged
6972
with pytest.raises(AssertionError, match=expected_error_msg):
7073
# ensure non-inplace call does not affect original
7174
tm.assert_categorical_equal(cat, expected)
72-
cat.replace(to_replace, value, inplace=True)
75+
76+
with tm.assert_produces_warning(FutureWarning, match="Series.replace"):
77+
# GH#44929 replace->_replace
78+
cat.replace(to_replace, value, inplace=True)
79+
7380
tm.assert_categorical_equal(cat, expected)

pandas/tests/series/methods/test_replace.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class TestSeriesReplace:
11-
def test_replace(self, datetime_series):
11+
def test_replace(self):
1212
N = 100
1313
ser = pd.Series(np.random.randn(N))
1414
ser[0:4] = np.nan
@@ -58,6 +58,7 @@ def test_replace(self, datetime_series):
5858
assert (ser[6:10] == -1).all()
5959
assert (ser[20:30] == -1).all()
6060

61+
def test_replace_nan_with_inf(self):
6162
ser = pd.Series([np.nan, 0, np.inf])
6263
tm.assert_series_equal(ser.replace(np.nan, 0), ser.fillna(0))
6364

@@ -67,6 +68,7 @@ def test_replace(self, datetime_series):
6768
filled[4] = 0
6869
tm.assert_series_equal(ser.replace(np.inf, 0), filled)
6970

71+
def test_replace_listlike_value_listlike_target(self, datetime_series):
7072
ser = pd.Series(datetime_series.index)
7173
tm.assert_series_equal(ser.replace(np.nan, 0), ser.fillna(0))
7274

0 commit comments

Comments
 (0)