Skip to content

Commit 0b05663

Browse files
committed
Improve readibility
1 parent d828a44 commit 0b05663

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Other Enhancements
115115
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`)
116116
- :func:`Styler.where` has been implemented. It is as a convenience for :func:`Styler.applymap` and enables simple DataFrame styling on the Jupyter notebook (:issue:`17474`).
117117
- :func:`MultiIndex.is_monotonic_decreasing` has been implemented. Previously returned ``False`` in all cases. (:issue:`16554`)
118-
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories`, and only updates the categories found in that dict. (:issue:`17336`)
118+
- :func:`Categorical.rename_categories` now accepts a dict-like argument as `new_categories` and only updates the categories found in that dict. (:issue:`17336`)
119119

120120

121121
.. _whatsnew_0210.api_breaking:

pandas/core/categorical.py

+1
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ def rename_categories(self, new_categories, inplace=False):
825825
"""
826826
inplace = validate_bool_kwarg(inplace, 'inplace')
827827
cat = self if inplace else self.copy()
828+
828829
if is_dict_like(new_categories):
829830
cat.categories = [new_categories.get(item, item)
830831
for item in cat.categories]

pandas/tests/test_categorical.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -989,26 +989,31 @@ def test_rename_categories_dict(self):
989989
res = cat.rename_categories({'a': 4, 'b': 3, 'c': 2, 'd': 1})
990990
expected = Index([4, 3, 2, 1])
991991
tm.assert_index_equal(res.categories, expected)
992+
992993
# Test for inplace
993994
res = cat.rename_categories({'a': 4, 'b': 3, 'c': 2, 'd': 1},
994995
inplace=True)
995996
assert res is None
996-
997997
tm.assert_index_equal(cat.categories, expected)
998+
998999
# Test for dicts of smaller length
9991000
cat = pd.Categorical(['a', 'b', 'c', 'd'])
10001001
res = cat.rename_categories({'a': 1, 'c': 3})
1002+
10011003
expected = Index([1, 'b', 3, 'd'])
10021004
tm.assert_index_equal(res.categories, expected)
1005+
10031006
# Test for dicts with bigger length
10041007
cat = pd.Categorical(['a', 'b', 'c', 'd'])
10051008
res = cat.rename_categories({'a': 1, 'b': 2, 'c': 3,
10061009
'd': 4, 'e': 5, 'f': 6})
10071010
expected = Index([1, 2, 3, 4])
10081011
tm.assert_index_equal(res.categories, expected)
1012+
10091013
# Test for dicts with no items from old categories
10101014
cat = pd.Categorical(['a', 'b', 'c', 'd'])
10111015
res = cat.rename_categories({'f': 1, 'g': 3})
1016+
10121017
expected = Index(['a', 'b', 'c', 'd'])
10131018
tm.assert_index_equal(res.categories, expected)
10141019

0 commit comments

Comments
 (0)