Skip to content

Commit 9be0b82

Browse files
authored
Deprecate inplace in Categorical.remove_categories (#37981)
1 parent 549e39b commit 9be0b82

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

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

613614
.. ---------------------------------------------------------------------------

pandas/core/arrays/categorical.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
Union,
1313
cast,
1414
)
15-
from warnings import warn
15+
from warnings import (
16+
catch_warnings,
17+
simplefilter,
18+
warn,
19+
)
1620

1721
import numpy as np
1822

@@ -1122,7 +1126,7 @@ def add_categories(self, new_categories, inplace=False):
11221126
if not inplace:
11231127
return cat
11241128

1125-
def remove_categories(self, removals, inplace=False):
1129+
def remove_categories(self, removals, inplace=no_default):
11261130
"""
11271131
Remove the specified categories.
11281132
@@ -1137,6 +1141,8 @@ def remove_categories(self, removals, inplace=False):
11371141
Whether or not to remove the categories inplace or return a copy of
11381142
this categorical with removed categories.
11391143
1144+
.. deprecated:: 1.3.0
1145+
11401146
Returns
11411147
-------
11421148
cat : Categorical or None
@@ -1155,6 +1161,18 @@ def remove_categories(self, removals, inplace=False):
11551161
remove_unused_categories : Remove categories which are not used.
11561162
set_categories : Set the categories to the specified ones.
11571163
"""
1164+
if inplace is not no_default:
1165+
warn(
1166+
"The `inplace` parameter in pandas.Categorical."
1167+
"remove_categories is deprecated and will be removed in "
1168+
"a future version. Removing unused categories will always "
1169+
"return a new Categorical object.",
1170+
FutureWarning,
1171+
stacklevel=2,
1172+
)
1173+
else:
1174+
inplace = False
1175+
11581176
inplace = validate_bool_kwarg(inplace, "inplace")
11591177
if not is_list_like(removals):
11601178
removals = [removals]
@@ -2355,14 +2373,20 @@ def replace(self, to_replace, value, inplace: bool = False):
23552373
continue
23562374
if replace_value in cat.categories:
23572375
if isna(new_value):
2358-
cat.remove_categories(replace_value, inplace=True)
2376+
with catch_warnings():
2377+
simplefilter("ignore")
2378+
cat.remove_categories(replace_value, inplace=True)
23592379
continue
2380+
23602381
categories = cat.categories.tolist()
23612382
index = categories.index(replace_value)
2383+
23622384
if new_value in cat.categories:
23632385
value_index = categories.index(new_value)
23642386
cat._codes[cat._codes == index] = value_index
2365-
cat.remove_categories(replace_value, inplace=True)
2387+
with catch_warnings():
2388+
simplefilter("ignore")
2389+
cat.remove_categories(replace_value, inplace=True)
23662390
else:
23672391
categories[index] = new_value
23682392
cat.rename_categories(categories, inplace=True)

pandas/tests/arrays/categorical/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ def test_validate_inplace_raises(self, value):
326326
cat.add_categories(new_categories=["D", "E", "F"], inplace=value)
327327

328328
with pytest.raises(ValueError, match=msg):
329-
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
329+
with tm.assert_produces_warning(FutureWarning):
330+
# issue #37643 inplace kwarg deprecated
331+
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
330332

331333
with pytest.raises(ValueError, match=msg):
332334
with tm.assert_produces_warning(FutureWarning):

pandas/tests/arrays/categorical/test_api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,10 @@ def test_remove_categories(self):
354354
tm.assert_categorical_equal(res, new)
355355

356356
# inplace == True
357-
res = cat.remove_categories("c", inplace=True)
357+
with tm.assert_produces_warning(FutureWarning):
358+
# issue #37643 inplace kwarg deprecated
359+
res = cat.remove_categories("c", inplace=True)
360+
358361
tm.assert_categorical_equal(cat, new)
359362
assert res is None
360363

0 commit comments

Comments
 (0)