Skip to content

Commit 5cd4ab8

Browse files
OlehKSSyeshsurya
authored andcommitted
Deprecate inplace in Categorical.reorder_categories. (pandas-dev#41133)
1 parent fc90331 commit 5cd4ab8

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

doc/source/whatsnew/v1.3.0.rst

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

615615
.. ---------------------------------------------------------------------------

pandas/core/arrays/categorical.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ def rename_categories(self, new_categories, inplace=False):
10311031
if not inplace:
10321032
return cat
10331033

1034-
def reorder_categories(self, new_categories, ordered=None, inplace=False):
1034+
def reorder_categories(self, new_categories, ordered=None, inplace=no_default):
10351035
"""
10361036
Reorder categories as specified in new_categories.
10371037
@@ -1049,6 +1049,8 @@ def reorder_categories(self, new_categories, ordered=None, inplace=False):
10491049
Whether or not to reorder the categories inplace or return a copy of
10501050
this categorical with reordered categories.
10511051
1052+
.. deprecated:: 1.3.0
1053+
10521054
Returns
10531055
-------
10541056
cat : Categorical or None
@@ -1068,6 +1070,18 @@ def reorder_categories(self, new_categories, ordered=None, inplace=False):
10681070
remove_unused_categories : Remove categories which are not used.
10691071
set_categories : Set the categories to the specified ones.
10701072
"""
1073+
if inplace is not no_default:
1074+
warn(
1075+
"The `inplace` parameter in pandas.Categorical."
1076+
"reorder_categories is deprecated and will be removed in "
1077+
"a future version. Removing unused categories will always "
1078+
"return a new Categorical object.",
1079+
FutureWarning,
1080+
stacklevel=2,
1081+
)
1082+
else:
1083+
inplace = False
1084+
10711085
inplace = validate_bool_kwarg(inplace, "inplace")
10721086
if set(self.dtype.categories) != set(new_categories):
10731087
raise ValueError(

pandas/tests/arrays/categorical/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ def test_validate_inplace_raises(self, value):
320320
cat.rename_categories(["X", "Y", "Z"], inplace=value)
321321

322322
with pytest.raises(ValueError, match=msg):
323-
cat.reorder_categories(["X", "Y", "Z"], ordered=True, inplace=value)
323+
with tm.assert_produces_warning(FutureWarning):
324+
# issue #37643 inplace kwarg deprecated
325+
cat.reorder_categories(["X", "Y", "Z"], ordered=True, inplace=value)
324326

325327
with pytest.raises(ValueError, match=msg):
326328
with tm.assert_produces_warning(FutureWarning):

pandas/tests/arrays/categorical/test_api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def test_reorder_categories(self):
153153
tm.assert_categorical_equal(res, new)
154154

155155
# inplace == True
156-
res = cat.reorder_categories(["c", "b", "a"], inplace=True)
156+
with tm.assert_produces_warning(FutureWarning):
157+
# issue #37643 inplace kwarg deprecated
158+
res = cat.reorder_categories(["c", "b", "a"], inplace=True)
159+
157160
assert res is None
158161
tm.assert_categorical_equal(cat, new)
159162

0 commit comments

Comments
 (0)