1212 Union ,
1313 cast ,
1414)
15- from warnings import warn
15+ from warnings import (
16+ catch_warnings ,
17+ simplefilter ,
18+ warn ,
19+ )
1620
1721import numpy as np
1822
@@ -1122,7 +1126,7 @@ def add_categories(self, new_categories, inplace=False):
11221126 if not inplace :
11231127 return cat
11241128
1125- def remove_categories (self , removals , inplace = False ):
1129+ def remove_categories (self , removals , inplace = no_default ):
11261130 """
11271131 Remove the specified categories.
11281132
@@ -1137,6 +1141,8 @@ def remove_categories(self, removals, inplace=False):
11371141 Whether or not to remove the categories inplace or return a copy of
11381142 this categorical with removed categories.
11391143
1144+ .. deprecated:: 1.3.0
1145+
11401146 Returns
11411147 -------
11421148 cat : Categorical or None
@@ -1155,6 +1161,18 @@ def remove_categories(self, removals, inplace=False):
11551161 remove_unused_categories : Remove categories which are not used.
11561162 set_categories : Set the categories to the specified ones.
11571163 """
1164+ if inplace is not no_default :
1165+ warn (
1166+ "The `inplace` parameter in pandas.Categorical."
1167+ "remove_categories is deprecated and will be removed in "
1168+ "a future version. Removing unused categories will always "
1169+ "return a new Categorical object." ,
1170+ FutureWarning ,
1171+ stacklevel = 2 ,
1172+ )
1173+ else :
1174+ inplace = False
1175+
11581176 inplace = validate_bool_kwarg (inplace , "inplace" )
11591177 if not is_list_like (removals ):
11601178 removals = [removals ]
@@ -2355,14 +2373,20 @@ def replace(self, to_replace, value, inplace: bool = False):
23552373 continue
23562374 if replace_value in cat .categories :
23572375 if isna (new_value ):
2358- cat .remove_categories (replace_value , inplace = True )
2376+ with catch_warnings ():
2377+ simplefilter ("ignore" )
2378+ cat .remove_categories (replace_value , inplace = True )
23592379 continue
2380+
23602381 categories = cat .categories .tolist ()
23612382 index = categories .index (replace_value )
2383+
23622384 if new_value in cat .categories :
23632385 value_index = categories .index (new_value )
23642386 cat ._codes [cat ._codes == index ] = value_index
2365- cat .remove_categories (replace_value , inplace = True )
2387+ with catch_warnings ():
2388+ simplefilter ("ignore" )
2389+ cat .remove_categories (replace_value , inplace = True )
23662390 else :
23672391 categories [index ] = new_value
23682392 cat .rename_categories (categories , inplace = True )
0 commit comments