Skip to content

Commit 56a2532

Browse files
authored
Deprecate inplace in Categorical.add_categories. (#41118)
1 parent 5ed44cb commit 56a2532

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` 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` 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
@@ -1075,7 +1075,7 @@ def reorder_categories(self, new_categories, ordered=None, inplace=False):
10751075
)
10761076
return self.set_categories(new_categories, ordered=ordered, inplace=inplace)
10771077

1078-
def add_categories(self, new_categories, inplace=False):
1078+
def add_categories(self, new_categories, inplace=no_default):
10791079
"""
10801080
Add new categories.
10811081
@@ -1090,6 +1090,8 @@ def add_categories(self, new_categories, inplace=False):
10901090
Whether or not to add the categories inplace or return a copy of
10911091
this categorical with added categories.
10921092
1093+
.. deprecated:: 1.3.0
1094+
10931095
Returns
10941096
-------
10951097
cat : Categorical or None
@@ -1109,6 +1111,18 @@ def add_categories(self, new_categories, inplace=False):
11091111
remove_unused_categories : Remove categories which are not used.
11101112
set_categories : Set the categories to the specified ones.
11111113
"""
1114+
if inplace is not no_default:
1115+
warn(
1116+
"The `inplace` parameter in pandas.Categorical."
1117+
"add_categories is deprecated and will be removed in "
1118+
"a future version. Removing unused categories will always "
1119+
"return a new Categorical object.",
1120+
FutureWarning,
1121+
stacklevel=2,
1122+
)
1123+
else:
1124+
inplace = False
1125+
11121126
inplace = validate_bool_kwarg(inplace, "inplace")
11131127
if not is_list_like(new_categories):
11141128
new_categories = [new_categories]

pandas/tests/arrays/categorical/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ def test_validate_inplace_raises(self, value):
323323
cat.reorder_categories(["X", "Y", "Z"], ordered=True, inplace=value)
324324

325325
with pytest.raises(ValueError, match=msg):
326-
cat.add_categories(new_categories=["D", "E", "F"], inplace=value)
326+
with tm.assert_produces_warning(FutureWarning):
327+
# issue #37643 inplace kwarg deprecated
328+
cat.add_categories(new_categories=["D", "E", "F"], inplace=value)
327329

328330
with pytest.raises(ValueError, match=msg):
329331
with tm.assert_produces_warning(FutureWarning):

pandas/tests/arrays/categorical/test_api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ def test_add_categories(self):
188188
tm.assert_categorical_equal(res, new)
189189

190190
# inplace == True
191-
res = cat.add_categories("d", inplace=True)
191+
with tm.assert_produces_warning(FutureWarning):
192+
# issue #37643 inplace kwarg deprecated
193+
res = cat.add_categories("d", inplace=True)
194+
192195
tm.assert_categorical_equal(cat, new)
193196
assert res is None
194197

0 commit comments

Comments
 (0)