Skip to content

Commit 973a3d6

Browse files
lukemanleynoatamir
authored andcommitted
DEPR: inplace arg in Categorical.remove_unused_categories (pandas-dev#49300)
deprecate Categorical.remove_unused_categories
1 parent f662e0e commit 973a3d6

File tree

5 files changed

+9
-65
lines changed

5 files changed

+9
-65
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Removal of prior version deprecations/changes
190190
- Removed argument ``tz`` from :meth:`Period.to_timestamp`, use ``obj.to_timestamp(...).tz_localize(tz)`` instead (:issue:`34522`)
191191
- Removed argument ``is_copy`` from :meth:`DataFrame.take` and :meth:`Series.take` (:issue:`30615`)
192192
- Removed argument ``kind`` from :meth:`Index.get_slice_bound`, :meth:`Index.slice_indexer` and :meth:`Index.slice_locs` (:issue:`41378`)
193+
- Removed argument ``inplace`` from :meth:`Categorical.remove_unused_categories` (:issue:`37918`)
193194
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
194195
- Remove keywords ``convert_float`` and ``mangle_dupe_cols`` from :func:`read_excel` (:issue:`41176`)
195196
- Disallow passing non-keyword arguments to :func:`read_excel` except ``io`` and ``sheet_name`` (:issue:`34418`)

pandas/core/arrays/categorical.py

+8-42
Original file line numberDiff line numberDiff line change
@@ -1393,35 +1393,14 @@ def remove_categories(self, removals, inplace=no_default):
13931393
new_categories, ordered=self.ordered, rename=False, inplace=inplace
13941394
)
13951395

1396-
@overload
1397-
def remove_unused_categories(
1398-
self, *, inplace: Literal[False] | NoDefault = ...
1399-
) -> Categorical:
1400-
...
1401-
1402-
@overload
1403-
def remove_unused_categories(self, *, inplace: Literal[True]) -> None:
1404-
...
1405-
1406-
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self"])
1407-
def remove_unused_categories(
1408-
self, inplace: bool | NoDefault = no_default
1409-
) -> Categorical | None:
1396+
def remove_unused_categories(self) -> Categorical:
14101397
"""
14111398
Remove categories which are not used.
14121399
1413-
Parameters
1414-
----------
1415-
inplace : bool, default False
1416-
Whether or not to drop unused categories inplace or return a copy of
1417-
this categorical with unused categories dropped.
1418-
1419-
.. deprecated:: 1.2.0
1420-
14211400
Returns
14221401
-------
1423-
cat : Categorical or None
1424-
Categorical with unused categories dropped or None if ``inplace=True``.
1402+
cat : Categorical
1403+
Categorical with unused categories dropped.
14251404
14261405
See Also
14271406
--------
@@ -1448,33 +1427,20 @@ def remove_unused_categories(
14481427
['a', 'c', 'a', 'c', 'c']
14491428
Categories (2, object): ['a', 'c']
14501429
"""
1451-
if inplace is not no_default:
1452-
warn(
1453-
"The `inplace` parameter in pandas.Categorical."
1454-
"remove_unused_categories is deprecated and "
1455-
"will be removed in a future version.",
1456-
FutureWarning,
1457-
stacklevel=find_stack_level(),
1458-
)
1459-
else:
1460-
inplace = False
1461-
1462-
inplace = validate_bool_kwarg(inplace, "inplace")
1463-
cat = self if inplace else self.copy()
1464-
idx, inv = np.unique(cat._codes, return_inverse=True)
1430+
idx, inv = np.unique(self._codes, return_inverse=True)
14651431

14661432
if idx.size != 0 and idx[0] == -1: # na sentinel
14671433
idx, inv = idx[1:], inv - 1
14681434

1469-
new_categories = cat.dtype.categories.take(idx)
1435+
new_categories = self.dtype.categories.take(idx)
14701436
new_dtype = CategoricalDtype._from_fastpath(
14711437
new_categories, ordered=self.ordered
14721438
)
14731439
new_codes = coerce_indexer_dtype(inv, new_dtype.categories)
1440+
1441+
cat = self.copy()
14741442
NDArrayBacked.__init__(cat, new_codes, new_dtype)
1475-
if not inplace:
1476-
return cat
1477-
return None
1443+
return cat
14781444

14791445
# ------------------------------------------------------------------
14801446

pandas/tests/arrays/categorical/test_analytics.py

-5
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,6 @@ def test_validate_inplace_raises(self, value):
363363
# issue #37643 inplace kwarg deprecated
364364
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
365365

366-
with pytest.raises(ValueError, match=msg):
367-
with tm.assert_produces_warning(FutureWarning):
368-
# issue #37643 inplace kwarg deprecated
369-
cat.remove_unused_categories(inplace=value)
370-
371366
with pytest.raises(ValueError, match=msg):
372367
cat.sort_values(inplace=value)
373368

pandas/tests/arrays/categorical/test_api.py

-7
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,6 @@ def test_remove_unused_categories(self):
422422
tm.assert_index_equal(res.categories, exp_categories_dropped)
423423
tm.assert_index_equal(c.categories, exp_categories_all)
424424

425-
with tm.assert_produces_warning(FutureWarning):
426-
# issue #37643 inplace kwarg deprecated
427-
res = c.remove_unused_categories(inplace=True)
428-
429-
tm.assert_index_equal(c.categories, exp_categories_dropped)
430-
assert res is None
431-
432425
# with NaN values (GH11599)
433426
c = Categorical(["a", "b", "c", np.nan], categories=["a", "b", "c", "d", "e"])
434427
res = c.remove_unused_categories()

pandas/tests/series/accessors/test_cat_accessor.py

-11
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,6 @@ def test_cat_accessor_no_new_attributes(self):
7878
with pytest.raises(AttributeError, match="You cannot add any new attribute"):
7979
cat.cat.xlabel = "a"
8080

81-
def test_cat_accessor_updates_on_inplace(self):
82-
ser = Series(list("abc")).astype("category")
83-
return_value = ser.drop(0, inplace=True)
84-
assert return_value is None
85-
86-
with tm.assert_produces_warning(FutureWarning):
87-
return_value = ser.cat.remove_unused_categories(inplace=True)
88-
89-
assert return_value is None
90-
assert len(ser.cat.categories) == 2
91-
9281
def test_categorical_delegations(self):
9382

9483
# invalid accessor

0 commit comments

Comments
 (0)