Skip to content

Commit 0a45fcc

Browse files
author
Oleh Kozynets
committed
Fix unit tests.
1 parent 08e4baf commit 0a45fcc

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ Deprecations
477477
- :meth:`Series.slice_shift` and :meth:`DataFrame.slice_shift` are deprecated, use :meth:`Series.shift` or :meth:`DataFrame.shift` instead (:issue:`37601`)
478478
- Partial slicing on unordered :class:`DatetimeIndex` with keys, which are not in Index is deprecated and will be removed in a future version (:issue:`18531`)
479479
- Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`DatetimeIndex`, :class:`TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`)
480-
- The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)
480+
- The ``inplace`` parameter of :meth:`Categorical.remove_categories`, :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)
481481

482482
.. ---------------------------------------------------------------------------
483483

pandas/core/arrays/categorical.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import operator
44
from shutil import get_terminal_size
55
from typing import Dict, Hashable, List, Sequence, Type, TypeVar, Union, cast
6-
from warnings import warn
6+
from warnings import catch_warnings, simplefilter, warn
77

88
import numpy as np
99

@@ -1016,7 +1016,7 @@ def add_categories(self, new_categories, inplace=False):
10161016
if not inplace:
10171017
return cat
10181018

1019-
def remove_categories(self, removals, inplace=False):
1019+
def remove_categories(self, removals, inplace=no_default):
10201020
"""
10211021
Remove the specified categories.
10221022
@@ -1031,6 +1031,8 @@ def remove_categories(self, removals, inplace=False):
10311031
Whether or not to remove the categories inplace or return a copy of
10321032
this categorical with removed categories.
10331033
1034+
.. deprecated:: 1.2.0
1035+
10341036
Returns
10351037
-------
10361038
cat : Categorical or None
@@ -1049,6 +1051,17 @@ def remove_categories(self, removals, inplace=False):
10491051
remove_unused_categories : Remove categories which are not used.
10501052
set_categories : Set the categories to the specified ones.
10511053
"""
1054+
if inplace is not no_default:
1055+
warn(
1056+
"The `inplace` parameter in pandas.Categorical."
1057+
"remove_categories is deprecated and "
1058+
"will be removed in a future version.",
1059+
FutureWarning,
1060+
stacklevel=2,
1061+
)
1062+
else:
1063+
inplace = False
1064+
10521065
inplace = validate_bool_kwarg(inplace, "inplace")
10531066
if not is_list_like(removals):
10541067
removals = [removals]
@@ -2289,14 +2302,20 @@ def replace(self, to_replace, value, inplace: bool = False):
22892302
continue
22902303
if replace_value in cat.categories:
22912304
if isna(new_value):
2292-
cat.remove_categories(replace_value, inplace=True)
2305+
with catch_warnings():
2306+
simplefilter("ignore")
2307+
cat.remove_categories(replace_value, inplace=True)
22932308
continue
2309+
22942310
categories = cat.categories.tolist()
22952311
index = categories.index(replace_value)
2312+
22962313
if new_value in cat.categories:
22972314
value_index = categories.index(new_value)
22982315
cat._codes[cat._codes == index] = value_index
2299-
cat.remove_categories(replace_value, inplace=True)
2316+
with catch_warnings():
2317+
simplefilter("ignore")
2318+
cat.remove_categories(replace_value, inplace=True)
23002319
else:
23012320
categories[index] = new_value
23022321
cat.rename_categories(categories, inplace=True)

pandas/tests/arrays/categorical/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ def test_validate_inplace_raises(self, value):
352352
cat.add_categories(new_categories=["D", "E", "F"], inplace=value)
353353

354354
with pytest.raises(ValueError, match=msg):
355-
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
355+
with tm.assert_produces_warning(FutureWarning):
356+
# issue #37643 inplace kwarg deprecated
357+
cat.remove_categories(removals=["D", "E", "F"], inplace=value)
356358

357359
with pytest.raises(ValueError, match=msg):
358360
with tm.assert_produces_warning(FutureWarning):

pandas/tests/arrays/categorical/test_api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ def test_remove_categories(self):
348348
tm.assert_categorical_equal(res, new)
349349

350350
# inplace == True
351-
res = cat.remove_categories("c", inplace=True)
351+
with tm.assert_produces_warning(FutureWarning):
352+
# issue #37643 inplace kwarg deprecated
353+
res = cat.remove_categories("c", inplace=True)
354+
352355
tm.assert_categorical_equal(cat, new)
353356
assert res is None
354357

0 commit comments

Comments
 (0)