Skip to content

Commit 971f4ff

Browse files
authored
Deprecate inplace in Categorical.remove_unused_categories (#37918)
1 parent 1319766 commit 971f4ff

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

doc/source/whatsnew/v1.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ Deprecations
475475
- :meth:`Categorical.is_dtype_equal` and :meth:`CategoricalIndex.is_dtype_equal` are deprecated, will be removed in a future version (:issue:`37545`)
476476
- :meth:`Series.slice_shift` and :meth:`DataFrame.slice_shift` are deprecated, use :meth:`Series.shift` or :meth:`DataFrame.shift` instead (:issue:`37601`)
477477
- Partial slicing on unordered :class:`DatetimeIndexes` with keys, which are not in Index is deprecated and will be removed in a future version (:issue:`18531`)
478+
- The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)
478479

479480
.. ---------------------------------------------------------------------------
480481

pandas/core/arrays/categorical.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pandas._config import get_option
1111

1212
from pandas._libs import NaT, algos as libalgos, hashtable as htable
13+
from pandas._libs.lib import no_default
1314
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
1415
from pandas.compat.numpy import function as nv
1516
from pandas.util._decorators import cache_readonly, deprecate_kwarg
@@ -1046,7 +1047,7 @@ def remove_categories(self, removals, inplace=False):
10461047
new_categories, ordered=self.ordered, rename=False, inplace=inplace
10471048
)
10481049

1049-
def remove_unused_categories(self, inplace=False):
1050+
def remove_unused_categories(self, inplace=no_default):
10501051
"""
10511052
Remove categories which are not used.
10521053
@@ -1056,6 +1057,8 @@ def remove_unused_categories(self, inplace=False):
10561057
Whether or not to drop unused categories inplace or return a copy of
10571058
this categorical with unused categories dropped.
10581059
1060+
.. deprecated:: 1.2.0
1061+
10591062
Returns
10601063
-------
10611064
cat : Categorical or None
@@ -1069,6 +1072,17 @@ def remove_unused_categories(self, inplace=False):
10691072
remove_categories : Remove the specified categories.
10701073
set_categories : Set the categories to the specified ones.
10711074
"""
1075+
if inplace is not no_default:
1076+
warn(
1077+
"The `inplace` parameter in pandas.Categorical."
1078+
"remove_unused_categories is deprecated and "
1079+
"will be removed in a future version.",
1080+
FutureWarning,
1081+
stacklevel=2,
1082+
)
1083+
else:
1084+
inplace = False
1085+
10721086
inplace = validate_bool_kwarg(inplace, "inplace")
10731087
cat = self if inplace else self.copy()
10741088
idx, inv = np.unique(cat._codes, return_inverse=True)

pandas/tests/arrays/categorical/test_analytics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ def test_validate_inplace_raises(self, value):
355355
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
356356

357357
with pytest.raises(ValueError, match=msg):
358-
cat.remove_unused_categories(inplace=value)
358+
with tm.assert_produces_warning(FutureWarning):
359+
# issue #37643 inplace kwarg deprecated
360+
cat.remove_unused_categories(inplace=value)
359361

360362
with pytest.raises(ValueError, match=msg):
361363
cat.sort_values(inplace=value)

pandas/tests/arrays/categorical/test_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ def test_remove_unused_categories(self):
371371
tm.assert_index_equal(res.categories, exp_categories_dropped)
372372
tm.assert_index_equal(c.categories, exp_categories_all)
373373

374-
res = c.remove_unused_categories(inplace=True)
374+
with tm.assert_produces_warning(FutureWarning):
375+
# issue #37643 inplace kwarg deprecated
376+
res = c.remove_unused_categories(inplace=True)
377+
375378
tm.assert_index_equal(c.categories, exp_categories_dropped)
376379
assert res is None
377380

pandas/tests/series/accessors/test_cat_accessor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def test_cat_accessor_updates_on_inplace(self):
8181
ser = Series(list("abc")).astype("category")
8282
return_value = ser.drop(0, inplace=True)
8383
assert return_value is None
84-
return_value = ser.cat.remove_unused_categories(inplace=True)
84+
85+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
86+
return_value = ser.cat.remove_unused_categories(inplace=True)
87+
8588
assert return_value is None
8689
assert len(ser.cat.categories) == 2
8790

0 commit comments

Comments
 (0)