-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Deprecate inplace in Categorical.remove_categories #37981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
0a45fcc
4b960f5
1439e84
d4c13f6
5ac507b
dc8a8d1
1e16941
be47fec
bed9a3a
3bcdd2b
d20ca42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,11 @@ | |
Union, | ||
cast, | ||
) | ||
from warnings import warn | ||
from warnings import ( | ||
catch_warnings, | ||
simplefilter, | ||
warn, | ||
) | ||
|
||
import numpy as np | ||
|
||
|
@@ -1125,7 +1129,7 @@ def add_categories(self, new_categories, inplace=False): | |
if not inplace: | ||
return cat | ||
|
||
def remove_categories(self, removals, inplace=False): | ||
def remove_categories(self, removals, inplace=no_default): | ||
""" | ||
Remove the specified categories. | ||
|
||
|
@@ -1140,6 +1144,8 @@ def remove_categories(self, removals, inplace=False): | |
Whether or not to remove the categories inplace or return a copy of | ||
this categorical with removed categories. | ||
|
||
.. deprecated:: 1.2.0 | ||
|
||
Returns | ||
------- | ||
cat : Categorical or None | ||
|
@@ -1158,6 +1164,17 @@ def remove_categories(self, removals, inplace=False): | |
remove_unused_categories : Remove categories which are not used. | ||
set_categories : Set the categories to the specified ones. | ||
""" | ||
if inplace is not no_default: | ||
warn( | ||
"The `inplace` parameter in pandas.Categorical." | ||
"remove_categories is deprecated and " | ||
"will be removed in a future version.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually we would say "do X instead", but since the idea is to never change the dtype inplace, might be worth adding a short sentence like "Removing unused categories will always return a new Categorical object" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I updated the warning message. |
||
FutureWarning, | ||
stacklevel=2, | ||
) | ||
else: | ||
inplace = False | ||
|
||
inplace = validate_bool_kwarg(inplace, "inplace") | ||
if not is_list_like(removals): | ||
removals = [removals] | ||
|
@@ -2357,14 +2374,20 @@ def replace(self, to_replace, value, inplace: bool = False): | |
continue | ||
if replace_value in cat.categories: | ||
if isna(new_value): | ||
cat.remove_categories(replace_value, inplace=True) | ||
with catch_warnings(): | ||
simplefilter("ignore") | ||
cat.remove_categories(replace_value, inplace=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think eventually we'll need to push this up and deprecate inplace here in Categorical.replace, but that can be done in a separate step |
||
continue | ||
|
||
categories = cat.categories.tolist() | ||
index = categories.index(replace_value) | ||
|
||
if new_value in cat.categories: | ||
value_index = categories.index(new_value) | ||
cat._codes[cat._codes == index] = value_index | ||
cat.remove_categories(replace_value, inplace=True) | ||
with catch_warnings(): | ||
simplefilter("ignore") | ||
cat.remove_categories(replace_value, inplace=True) | ||
else: | ||
categories[index] = new_value | ||
cat.rename_categories(categories, inplace=True) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -360,7 +360,9 @@ def test_validate_inplace_raises(self, value): | |
cat.add_categories(new_categories=["D", "E", "F"], inplace=value) | ||
|
||
with pytest.raises(ValueError, match=msg): | ||
cat.remove_categories(removals=["D", "E", "F"], inplace=value) | ||
with tm.assert_produces_warning(FutureWarning): | ||
# issue #37643 inplace kwarg deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd drop this comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yah i find these helpful |
||
cat.remove_categories(removals=["D", "E", "F"], inplace=value) | ||
|
||
with pytest.raises(ValueError, match=msg): | ||
with tm.assert_produces_warning(FutureWarning): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,7 +354,10 @@ def test_remove_categories(self): | |
tm.assert_categorical_equal(res, new) | ||
|
||
# inplace == True | ||
res = cat.remove_categories("c", inplace=True) | ||
with tm.assert_produces_warning(FutureWarning): | ||
# issue #37643 inplace kwarg deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
res = cat.remove_categories("c", inplace=True) | ||
|
||
tm.assert_categorical_equal(cat, new) | ||
assert res is None | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.3.0 now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, thanks. I updated the version here and fixed conflicts with the master