10
10
from pandas ._config import get_option
11
11
12
12
from pandas ._libs import NaT , algos as libalgos , hashtable as htable
13
+ from pandas ._libs .lib import no_default
13
14
from pandas ._typing import ArrayLike , Dtype , Ordered , Scalar
14
15
from pandas .compat .numpy import function as nv
15
16
from pandas .util ._decorators import cache_readonly , deprecate_kwarg
@@ -1046,7 +1047,7 @@ def remove_categories(self, removals, inplace=False):
1046
1047
new_categories , ordered = self .ordered , rename = False , inplace = inplace
1047
1048
)
1048
1049
1049
- def remove_unused_categories (self , inplace = False ):
1050
+ def remove_unused_categories (self , inplace = no_default ):
1050
1051
"""
1051
1052
Remove categories which are not used.
1052
1053
@@ -1056,10 +1057,12 @@ def remove_unused_categories(self, inplace=False):
1056
1057
Whether or not to drop unused categories inplace or return a copy of
1057
1058
this categorical with unused categories dropped.
1058
1059
1060
+ .. deprecated:: 1.2.0
1061
+
1059
1062
Returns
1060
1063
-------
1061
1064
cat : Categorical or None
1062
- Categorical with unused categories dropped or None if ``inplace=True`` .
1065
+ Categorical with unused categories dropped.
1063
1066
1064
1067
See Also
1065
1068
--------
@@ -1069,8 +1072,14 @@ def remove_unused_categories(self, inplace=False):
1069
1072
remove_categories : Remove the specified categories.
1070
1073
set_categories : Set the categories to the specified ones.
1071
1074
"""
1072
- inplace = validate_bool_kwarg (inplace , "inplace" )
1073
- cat = self if inplace else self .copy ()
1075
+ if inplace is not no_default :
1076
+ warn (
1077
+ "The `inplace` parameter in pandas.Categorical.remove_unused_categories"
1078
+ " is deprecated and will be removed in a future version." ,
1079
+ FutureWarning , stacklevel = 2
1080
+ )
1081
+
1082
+ cat = self .copy ()
1074
1083
idx , inv = np .unique (cat ._codes , return_inverse = True )
1075
1084
1076
1085
if idx .size != 0 and idx [0 ] == - 1 : # na sentinel
@@ -1083,8 +1092,7 @@ def remove_unused_categories(self, inplace=False):
1083
1092
cat ._dtype = new_dtype
1084
1093
cat ._codes = coerce_indexer_dtype (inv , new_dtype .categories )
1085
1094
1086
- if not inplace :
1087
- return cat
1095
+ return cat
1088
1096
1089
1097
# ------------------------------------------------------------------
1090
1098
0 commit comments