Skip to content

Commit c556c20

Browse files
authored
Deprecate inplace in Categorical.rename_categories (#41186)
1 parent ad717c7 commit c556c20

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ Deprecations
609609
- Deprecated using :func:`merge` or :func:`join` on a different number of levels (:issue:`34862`)
610610
- Deprecated the use of ``**kwargs`` in :class:`.ExcelWriter`; use the keyword argument ``engine_kwargs`` instead (:issue:`40430`)
611611
- Deprecated the ``level`` keyword for :class:`DataFrame` and :class:`Series` aggregations; use groupby instead (:issue:`39983`)
612-
- The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.add_categories`, :meth:`Categorical.reorder_categories` is deprecated and will be removed in a future version (:issue:`37643`)
612+
- The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.add_categories`, :meth:`Categorical.reorder_categories`, :meth:`Categorical.rename_categories` is deprecated and will be removed in a future version (:issue:`37643`)
613613
- Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`)
614614

615615
.. ---------------------------------------------------------------------------

pandas/core/arrays/categorical.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ def set_categories(self, new_categories, ordered=None, rename=False, inplace=Fal
955955
if not inplace:
956956
return cat
957957

958-
def rename_categories(self, new_categories, inplace=False):
958+
def rename_categories(self, new_categories, inplace=no_default):
959959
"""
960960
Rename categories.
961961
@@ -980,6 +980,8 @@ def rename_categories(self, new_categories, inplace=False):
980980
Whether or not to rename the categories inplace or return a copy of
981981
this categorical with renamed categories.
982982
983+
.. deprecated:: 1.3.0
984+
983985
Returns
984986
-------
985987
cat : Categorical or None
@@ -1019,6 +1021,18 @@ def rename_categories(self, new_categories, inplace=False):
10191021
['A', 'A', 'B']
10201022
Categories (2, object): ['A', 'B']
10211023
"""
1024+
if inplace is not no_default:
1025+
warn(
1026+
"The `inplace` parameter in pandas.Categorical."
1027+
"rename_categories is deprecated and will be removed in "
1028+
"a future version. Removing unused categories will always "
1029+
"return a new Categorical object.",
1030+
FutureWarning,
1031+
stacklevel=2,
1032+
)
1033+
else:
1034+
inplace = False
1035+
10221036
inplace = validate_bool_kwarg(inplace, "inplace")
10231037
cat = self if inplace else self.copy()
10241038

@@ -2417,7 +2431,9 @@ def replace(self, to_replace, value, inplace: bool = False):
24172431
cat.remove_categories(replace_value, inplace=True)
24182432
else:
24192433
categories[index] = new_value
2420-
cat.rename_categories(categories, inplace=True)
2434+
with catch_warnings():
2435+
simplefilter("ignore")
2436+
cat.rename_categories(categories, inplace=True)
24212437
if not inplace:
24222438
return cat
24232439

pandas/tests/arrays/categorical/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ def test_validate_inplace_raises(self, value):
317317
cat.set_categories(["X", "Y", "Z"], rename=True, inplace=value)
318318

319319
with pytest.raises(ValueError, match=msg):
320-
cat.rename_categories(["X", "Y", "Z"], inplace=value)
320+
with tm.assert_produces_warning(FutureWarning):
321+
# issue #37643 inplace kwarg deprecated
322+
cat.rename_categories(["X", "Y", "Z"], inplace=value)
321323

322324
with pytest.raises(ValueError, match=msg):
323325
with tm.assert_produces_warning(FutureWarning):

pandas/tests/arrays/categorical/test_api.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ def test_rename_categories(self):
8282
tm.assert_categorical_equal(result, expected)
8383

8484
# and now inplace
85-
res = cat.rename_categories([1, 2, 3], inplace=True)
85+
with tm.assert_produces_warning(FutureWarning):
86+
# issue #37643 inplace kwarg deprecated
87+
res = cat.rename_categories([1, 2, 3], inplace=True)
88+
8689
assert res is None
8790
tm.assert_numpy_array_equal(
8891
cat.__array__(), np.array([1, 2, 3, 1], dtype=np.int64)
@@ -114,7 +117,10 @@ def test_rename_categories_dict(self):
114117
tm.assert_index_equal(res.categories, expected)
115118

116119
# Test for inplace
117-
res = cat.rename_categories({"a": 4, "b": 3, "c": 2, "d": 1}, inplace=True)
120+
with tm.assert_produces_warning(FutureWarning):
121+
# issue #37643 inplace kwarg deprecated
122+
res = cat.rename_categories({"a": 4, "b": 3, "c": 2, "d": 1}, inplace=True)
123+
118124
assert res is None
119125
tm.assert_index_equal(cat.categories, expected)
120126

0 commit comments

Comments
 (0)