diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index a286d152f03c3..c4e290f67e5b9 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -607,6 +607,7 @@ Deprecations - Deprecated using :func:`merge` or :func:`join` on a different number of levels (:issue:`34862`) - Deprecated the use of ``**kwargs`` in :class:`.ExcelWriter`; use the keyword argument ``engine_kwargs`` instead (:issue:`40430`) - Deprecated the ``level`` keyword for :class:`DataFrame` and :class:`Series` aggregations; use groupby instead (:issue:`39983`) +- The ``inplace`` parameter of :meth:`Categorical.remove_categories` is deprecated and will be removed in a future version (:issue:`37643`) - Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index c51e25776e1c2..853b1b38a444b 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -12,7 +12,11 @@ Union, cast, ) -from warnings import warn +from warnings import ( + catch_warnings, + simplefilter, + warn, +) import numpy as np @@ -1122,7 +1126,7 @@ def add_categories(self, new_categories, inplace=False): if not inplace: return cat - def remove_categories(self, removals, inplace=False): + def remove_categories(self, removals, inplace=no_default): """ Remove the specified categories. @@ -1137,6 +1141,8 @@ def remove_categories(self, removals, inplace=False): Whether or not to remove the categories inplace or return a copy of this categorical with removed categories. + .. deprecated:: 1.3.0 + Returns ------- cat : Categorical or None @@ -1155,6 +1161,18 @@ def remove_categories(self, removals, inplace=False): remove_unused_categories : Remove categories which are not used. set_categories : Set the categories to the specified ones. """ + if inplace is not no_default: + warn( + "The `inplace` parameter in pandas.Categorical." + "remove_categories is deprecated and will be removed in " + "a future version. Removing unused categories will always " + "return a new Categorical object.", + FutureWarning, + stacklevel=2, + ) + else: + inplace = False + inplace = validate_bool_kwarg(inplace, "inplace") if not is_list_like(removals): removals = [removals] @@ -2355,14 +2373,20 @@ def replace(self, to_replace, value, inplace: bool = False): continue if replace_value in cat.categories: if isna(new_value): - cat.remove_categories(replace_value, inplace=True) + with catch_warnings(): + simplefilter("ignore") + cat.remove_categories(replace_value, inplace=True) continue + categories = cat.categories.tolist() index = categories.index(replace_value) + if new_value in cat.categories: value_index = categories.index(new_value) cat._codes[cat._codes == index] = value_index - cat.remove_categories(replace_value, inplace=True) + with catch_warnings(): + simplefilter("ignore") + cat.remove_categories(replace_value, inplace=True) else: categories[index] = new_value cat.rename_categories(categories, inplace=True) diff --git a/pandas/tests/arrays/categorical/test_analytics.py b/pandas/tests/arrays/categorical/test_analytics.py index 56d474497a166..37f04e5d30a66 100644 --- a/pandas/tests/arrays/categorical/test_analytics.py +++ b/pandas/tests/arrays/categorical/test_analytics.py @@ -326,7 +326,9 @@ def test_validate_inplace_raises(self, value): cat.add_categories(new_categories=["D", "E", "F"], inplace=value) with pytest.raises(ValueError, match=msg): - cat.remove_categories(removals=["D", "E", "F"], inplace=value) + with tm.assert_produces_warning(FutureWarning): + # issue #37643 inplace kwarg deprecated + cat.remove_categories(removals=["D", "E", "F"], inplace=value) with pytest.raises(ValueError, match=msg): with tm.assert_produces_warning(FutureWarning): diff --git a/pandas/tests/arrays/categorical/test_api.py b/pandas/tests/arrays/categorical/test_api.py index a6dea639488a2..b6719d61ffc3c 100644 --- a/pandas/tests/arrays/categorical/test_api.py +++ b/pandas/tests/arrays/categorical/test_api.py @@ -354,7 +354,10 @@ def test_remove_categories(self): tm.assert_categorical_equal(res, new) # inplace == True - res = cat.remove_categories("c", inplace=True) + with tm.assert_produces_warning(FutureWarning): + # issue #37643 inplace kwarg deprecated + res = cat.remove_categories("c", inplace=True) + tm.assert_categorical_equal(cat, new) assert res is None