12
12
Union ,
13
13
cast ,
14
14
)
15
- from warnings import warn
15
+ from warnings import (
16
+ catch_warnings ,
17
+ simplefilter ,
18
+ warn ,
19
+ )
16
20
17
21
import numpy as np
18
22
@@ -1122,7 +1126,7 @@ def add_categories(self, new_categories, inplace=False):
1122
1126
if not inplace :
1123
1127
return cat
1124
1128
1125
- def remove_categories (self , removals , inplace = False ):
1129
+ def remove_categories (self , removals , inplace = no_default ):
1126
1130
"""
1127
1131
Remove the specified categories.
1128
1132
@@ -1137,6 +1141,8 @@ def remove_categories(self, removals, inplace=False):
1137
1141
Whether or not to remove the categories inplace or return a copy of
1138
1142
this categorical with removed categories.
1139
1143
1144
+ .. deprecated:: 1.3.0
1145
+
1140
1146
Returns
1141
1147
-------
1142
1148
cat : Categorical or None
@@ -1155,6 +1161,18 @@ def remove_categories(self, removals, inplace=False):
1155
1161
remove_unused_categories : Remove categories which are not used.
1156
1162
set_categories : Set the categories to the specified ones.
1157
1163
"""
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
+
1158
1176
inplace = validate_bool_kwarg (inplace , "inplace" )
1159
1177
if not is_list_like (removals ):
1160
1178
removals = [removals ]
@@ -2355,14 +2373,20 @@ def replace(self, to_replace, value, inplace: bool = False):
2355
2373
continue
2356
2374
if replace_value in cat .categories :
2357
2375
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 )
2359
2379
continue
2380
+
2360
2381
categories = cat .categories .tolist ()
2361
2382
index = categories .index (replace_value )
2383
+
2362
2384
if new_value in cat .categories :
2363
2385
value_index = categories .index (new_value )
2364
2386
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 )
2366
2390
else :
2367
2391
categories [index ] = new_value
2368
2392
cat .rename_categories (categories , inplace = True )
0 commit comments