Skip to content

Commit 88ce933

Browse files
authored
Deprecate inplace in Categorical.set_categories. (#41307)
1 parent bb636d9 commit 88ce933

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

doc/source/user_guide/categorical.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ categorical (categories and ordering). So if you read back the CSV file you have
954954
relevant columns back to ``category`` and assign the right categories and categories ordering.
955955

956956
.. ipython:: python
957+
:okwarning:
957958
958959
import io
959960

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ Deprecations
639639
- Deprecated using :func:`merge` or :func:`join` on a different number of levels (:issue:`34862`)
640640
- Deprecated the use of ``**kwargs`` in :class:`.ExcelWriter`; use the keyword argument ``engine_kwargs`` instead (:issue:`40430`)
641641
- Deprecated the ``level`` keyword for :class:`DataFrame` and :class:`Series` aggregations; use groupby instead (:issue:`39983`)
642-
- The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.add_categories`, :meth:`Categorical.reorder_categories`, :meth:`Categorical.rename_categories` is deprecated and will be removed in a future version (:issue:`37643`)
642+
- The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.add_categories`, :meth:`Categorical.reorder_categories`, :meth:`Categorical.rename_categories`, :meth:`Categorical.set_categories` is deprecated and will be removed in a future version (:issue:`37643`)
643643
- Deprecated :func:`merge` producing duplicated columns through the ``suffixes`` keyword and already existing columns (:issue:`22818`)
644644

645645
.. ---------------------------------------------------------------------------

pandas/core/arrays/categorical.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,9 @@ def as_unordered(self, inplace=False):
882882
inplace = validate_bool_kwarg(inplace, "inplace")
883883
return self.set_ordered(False, inplace=inplace)
884884

885-
def set_categories(self, new_categories, ordered=None, rename=False, inplace=False):
885+
def set_categories(
886+
self, new_categories, ordered=None, rename=False, inplace=no_default
887+
):
886888
"""
887889
Set the categories to the specified new_categories.
888890
@@ -916,6 +918,8 @@ def set_categories(self, new_categories, ordered=None, rename=False, inplace=Fal
916918
Whether or not to reorder the categories in-place or return a copy
917919
of this categorical with reordered categories.
918920
921+
.. deprecated:: 1.3.0
922+
919923
Returns
920924
-------
921925
Categorical with reordered categories or None if inplace.
@@ -933,6 +937,18 @@ def set_categories(self, new_categories, ordered=None, rename=False, inplace=Fal
933937
remove_categories : Remove the specified categories.
934938
remove_unused_categories : Remove categories which are not used.
935939
"""
940+
if inplace is not no_default:
941+
warn(
942+
"The `inplace` parameter in pandas.Categorical."
943+
"set_categories is deprecated and will be removed in "
944+
"a future version. Removing unused categories will always "
945+
"return a new Categorical object.",
946+
FutureWarning,
947+
stacklevel=2,
948+
)
949+
else:
950+
inplace = False
951+
936952
inplace = validate_bool_kwarg(inplace, "inplace")
937953
if ordered is None:
938954
ordered = self.dtype.ordered
@@ -1101,7 +1117,10 @@ def reorder_categories(self, new_categories, ordered=None, inplace=no_default):
11011117
raise ValueError(
11021118
"items in new_categories are not the same as in old categories"
11031119
)
1104-
return self.set_categories(new_categories, ordered=ordered, inplace=inplace)
1120+
1121+
with catch_warnings():
1122+
simplefilter("ignore")
1123+
return self.set_categories(new_categories, ordered=ordered, inplace=inplace)
11051124

11061125
def add_categories(self, new_categories, inplace=no_default):
11071126
"""
@@ -1231,9 +1250,11 @@ def remove_categories(self, removals, inplace=no_default):
12311250
if len(not_included) != 0:
12321251
raise ValueError(f"removals must all be in old categories: {not_included}")
12331252

1234-
return self.set_categories(
1235-
new_categories, ordered=self.ordered, rename=False, inplace=inplace
1236-
)
1253+
with catch_warnings():
1254+
simplefilter("ignore")
1255+
return self.set_categories(
1256+
new_categories, ordered=self.ordered, rename=False, inplace=inplace
1257+
)
12371258

12381259
def remove_unused_categories(self, inplace=no_default):
12391260
"""

pandas/tests/arrays/categorical/test_analytics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ def test_validate_inplace_raises(self, value):
314314
cat.as_unordered(inplace=value)
315315

316316
with pytest.raises(ValueError, match=msg):
317-
cat.set_categories(["X", "Y", "Z"], rename=True, inplace=value)
317+
with tm.assert_produces_warning(FutureWarning):
318+
# issue #37643 inplace kwarg deprecated
319+
cat.set_categories(["X", "Y", "Z"], rename=True, inplace=value)
318320

319321
with pytest.raises(ValueError, match=msg):
320322
with tm.assert_produces_warning(FutureWarning):

pandas/tests/arrays/categorical/test_api.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,10 @@ def test_set_categories(self):
229229
exp_categories = Index(["c", "b", "a"])
230230
exp_values = np.array(["a", "b", "c", "a"], dtype=np.object_)
231231

232-
res = cat.set_categories(["c", "b", "a"], inplace=True)
232+
with tm.assert_produces_warning(FutureWarning):
233+
# issue #37643 inplace kwarg deprecated
234+
res = cat.set_categories(["c", "b", "a"], inplace=True)
235+
233236
tm.assert_index_equal(cat.categories, exp_categories)
234237
tm.assert_numpy_array_equal(cat.__array__(), exp_values)
235238
assert res is None
@@ -439,7 +442,11 @@ def test_describe(self):
439442

440443
# check unused categories
441444
cat = self.factor.copy()
442-
cat.set_categories(["a", "b", "c", "d"], inplace=True)
445+
446+
with tm.assert_produces_warning(FutureWarning):
447+
# issue #37643 inplace kwarg deprecated
448+
cat.set_categories(["a", "b", "c", "d"], inplace=True)
449+
443450
desc = cat.describe()
444451

445452
exp_index = CategoricalIndex(
@@ -475,7 +482,11 @@ def test_describe(self):
475482

476483
def test_set_categories_inplace(self):
477484
cat = self.factor.copy()
478-
cat.set_categories(["a", "b", "c", "d"], inplace=True)
485+
486+
with tm.assert_produces_warning(FutureWarning):
487+
# issue #37643 inplace kwarg deprecated
488+
cat.set_categories(["a", "b", "c", "d"], inplace=True)
489+
479490
tm.assert_index_equal(cat.categories, Index(["a", "b", "c", "d"]))
480491

481492

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,11 @@ def test_setitem_mask_categorical(self):
887887
df = DataFrame({"cats": catsf, "values": valuesf}, index=idxf)
888888

889889
exp_fancy = exp_multi_row.copy()
890-
return_value = exp_fancy["cats"].cat.set_categories(
891-
["a", "b", "c"], inplace=True
892-
)
890+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
891+
# issue #37643 inplace kwarg deprecated
892+
return_value = exp_fancy["cats"].cat.set_categories(
893+
["a", "b", "c"], inplace=True
894+
)
893895
assert return_value is None
894896

895897
mask = df["cats"] == "c"

pandas/tests/series/accessors/test_cat_accessor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def test_cat_accessor(self):
4848
assert not ser.cat.ordered, False
4949

5050
exp = Categorical(["a", "b", np.nan, "a"], categories=["b", "a"])
51-
return_value = ser.cat.set_categories(["b", "a"], inplace=True)
51+
52+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
53+
# issue #37643 inplace kwarg deprecated
54+
return_value = ser.cat.set_categories(["b", "a"], inplace=True)
55+
5256
assert return_value is None
5357
tm.assert_categorical_equal(ser.values, exp)
5458

0 commit comments

Comments
 (0)