-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Allow for dict-like argument to Categorical.rename_categories #17586
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 2 commits
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 |
---|---|---|
|
@@ -25,7 +25,8 @@ | |
is_categorical_dtype, | ||
is_integer_dtype, is_bool, | ||
is_list_like, is_sequence, | ||
is_scalar) | ||
is_scalar, | ||
is_dict_like) | ||
from pandas.core.common import is_null_slice, _maybe_box_datetimelike | ||
|
||
from pandas.core.algorithms import factorize, take_1d, unique1d | ||
|
@@ -824,7 +825,12 @@ def rename_categories(self, new_categories, inplace=False): | |
""" | ||
inplace = validate_bool_kwarg(inplace, 'inplace') | ||
cat = self if inplace else self.copy() | ||
cat.categories = new_categories | ||
|
||
if is_dict_like(new_categories): | ||
cat.categories = [new_categories.get(item, item) | ||
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. actually make sure the doc string is updated here as well (maybe add a versionchanged tag) |
||
for item in cat.categories] | ||
else: | ||
cat.categories = new_categories | ||
if not inplace: | ||
return cat | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -983,6 +983,40 @@ def test_rename_categories(self): | |
with pytest.raises(ValueError): | ||
cat.rename_categories([1, 2]) | ||
|
||
def test_rename_categories_dict(self): | ||
# GH 17336 | ||
cat = pd.Categorical(['a', 'b', 'c', 'd']) | ||
res = cat.rename_categories({'a': 4, 'b': 3, 'c': 2, 'd': 1}) | ||
expected = Index([4, 3, 2, 1]) | ||
tm.assert_index_equal(res.categories, expected) | ||
|
||
# Test for inplace | ||
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: newline above this one. |
||
res = cat.rename_categories({'a': 4, 'b': 3, 'c': 2, 'd': 1}, | ||
inplace=True) | ||
assert res is None | ||
tm.assert_index_equal(cat.categories, expected) | ||
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: remove newline above this one. |
||
|
||
# Test for dicts of smaller length | ||
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. blank lines between sub-tests 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: newline above this one. |
||
cat = pd.Categorical(['a', 'b', 'c', 'd']) | ||
res = cat.rename_categories({'a': 1, 'c': 3}) | ||
|
||
expected = Index([1, 'b', 3, 'd']) | ||
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: newline above this one. |
||
tm.assert_index_equal(res.categories, expected) | ||
|
||
# Test for dicts with bigger length | ||
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: newline above this one. |
||
cat = pd.Categorical(['a', 'b', 'c', 'd']) | ||
res = cat.rename_categories({'a': 1, 'b': 2, 'c': 3, | ||
'd': 4, 'e': 5, 'f': 6}) | ||
expected = Index([1, 2, 3, 4]) | ||
tm.assert_index_equal(res.categories, expected) | ||
|
||
# Test for dicts with no items from old categories | ||
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: newline above this one. |
||
cat = pd.Categorical(['a', 'b', 'c', 'd']) | ||
res = cat.rename_categories({'f': 1, 'g': 3}) | ||
|
||
expected = Index(['a', 'b', 'c', 'd']) | ||
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: newline above this one. |
||
tm.assert_index_equal(res.categories, expected) | ||
|
||
@pytest.mark.parametrize('codes, old, new, expected', [ | ||
([0, 1], ['a', 'b'], ['a', 'b'], [0, 1]), | ||
([0, 1], ['b', 'a'], ['b', 'a'], [0, 1]), | ||
|
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.
Nit: newline above this one.