Skip to content

Commit 1499e88

Browse files
author
Oleh Kozynets
committed
Deprecate inplace in Categorical.remove_unused_categories.
1 parent 91f5bfc commit 1499e88

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

pandas/core/arrays/categorical.py

+14-6
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,10 +1057,12 @@ 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
1062-
Categorical with unused categories dropped or None if ``inplace=True``.
1065+
Categorical with unused categories dropped.
10631066
10641067
See Also
10651068
--------
@@ -1069,8 +1072,14 @@ 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
"""
1072-
inplace = validate_bool_kwarg(inplace, "inplace")
1073-
cat = self if inplace else self.copy()
1075+
if inplace is not no_default:
1076+
warn(
1077+
"The `inplace` parameter in pandas.Categorical.remove_unused_categories"
1078+
" is deprecated and will be removed in a future version.",
1079+
FutureWarning, stacklevel=2
1080+
)
1081+
1082+
cat = self.copy()
10741083
idx, inv = np.unique(cat._codes, return_inverse=True)
10751084

10761085
if idx.size != 0 and idx[0] == -1: # na sentinel
@@ -1083,8 +1092,7 @@ def remove_unused_categories(self, inplace=False):
10831092
cat._dtype = new_dtype
10841093
cat._codes = coerce_indexer_dtype(inv, new_dtype.categories)
10851094

1086-
if not inplace:
1087-
return cat
1095+
return cat
10881096

10891097
# ------------------------------------------------------------------
10901098

pandas/tests/arrays/categorical/test_analytics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def test_validate_inplace_raises(self, value):
354354
with pytest.raises(ValueError, match=msg):
355355
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
356356

357-
with pytest.raises(ValueError, match=msg):
357+
with tm.assert_produces_warning(FutureWarning):
358358
cat.remove_unused_categories(inplace=value)
359359

360360
with pytest.raises(ValueError, match=msg):

pandas/tests/arrays/categorical/test_api.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,8 @@ 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)
375-
tm.assert_index_equal(c.categories, exp_categories_dropped)
376-
assert res is None
374+
with tm.assert_produces_warning(FutureWarning):
375+
c.remove_unused_categories(inplace=True)
377376

378377
# with NaN values (GH11599)
379378
c = Categorical(["a", "b", "c", np.nan], categories=["a", "b", "c", "d", "e"])

0 commit comments

Comments
 (0)