Skip to content

Deprecate inplace in Categorical.reorder_categories. #41133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,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`, :meth:`Categorical.add_categories` is deprecated and will be removed in a future version (:issue:`37643`)
- 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`)
- Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`)

.. ---------------------------------------------------------------------------
Expand Down
16 changes: 15 additions & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ def rename_categories(self, new_categories, inplace=False):
if not inplace:
return cat

def reorder_categories(self, new_categories, ordered=None, inplace=False):
def reorder_categories(self, new_categories, ordered=None, inplace=no_default):
"""
Reorder categories as specified in new_categories.

Expand All @@ -1049,6 +1049,8 @@ def reorder_categories(self, new_categories, ordered=None, inplace=False):
Whether or not to reorder the categories inplace or return a copy of
this categorical with reordered categories.

.. deprecated:: 1.3.0

Returns
-------
cat : Categorical or None
Expand All @@ -1068,6 +1070,18 @@ def reorder_categories(self, new_categories, ordered=None, 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."
"reorder_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 set(self.dtype.categories) != set(new_categories):
raise ValueError(
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ def test_validate_inplace_raises(self, value):
cat.rename_categories(["X", "Y", "Z"], inplace=value)

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

with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning):
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def test_reorder_categories(self):
tm.assert_categorical_equal(res, new)

# inplace == True
res = cat.reorder_categories(["c", "b", "a"], inplace=True)
with tm.assert_produces_warning(FutureWarning):
# issue #37643 inplace kwarg deprecated
res = cat.reorder_categories(["c", "b", "a"], inplace=True)

assert res is None
tm.assert_categorical_equal(cat, new)

Expand Down