Skip to content

ENH: Let Categorical.rename_categories take a callable #18862

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

Merged
merged 2 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ Other Enhancements
- :func:`read_excel()` has gained the ``nrows`` parameter (:issue:`16645`)
- :func:``DataFrame.to_json`` and ``Series.to_json`` now accept an ``index`` argument which allows the user to exclude the index from the JSON output (:issue:`17394`)
- ``IntervalIndex.to_tuples()`` has gained the ``na_tuple`` parameter to control whether NA is returned as a tuple of NA, or NA itself (:issue:`18756`)
- ``Categorical.rename_categories``, ``CategoricalIndex.rename_categories`` and :attr:`Series.cat.rename_categories`
can now take a callable as their argument (:issue:`18862`)

.. _whatsnew_0220.api_breaking:

Expand Down
19 changes: 17 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,15 +844,22 @@ def rename_categories(self, new_categories, inplace=False):

Parameters
----------
new_categories : list-like or dict-like
new_categories : list-like, dict-like or callable

* list-like: all items must be unique and the number of items in
the new categories must match the existing number of categories.

* dict-like: specifies a mapping from
old categories to new. Categories not contained in the mapping
are passed through and extra categories in the mapping are
ignored. *New in version 0.21.0*.
ignored.

.. versionadded:: 0.21.0

* callable : a callable that is called on all items in the old
categories and whose return values comprise the new categories.

.. versionadded:: 0.22.0

.. warning::

Expand Down Expand Up @@ -890,6 +897,12 @@ def rename_categories(self, new_categories, inplace=False):
>>> c.rename_categories({'a': 'A', 'c': 'C'})
[A, A, b]
Categories (2, object): [A, b]

You may also provide a callable to create the new categories

>>> c.rename_categories(lambda x: x.upper())
[A, A, B]
Categories (2, object): [A, B]
"""
inplace = validate_bool_kwarg(inplace, 'inplace')
cat = self if inplace else self.copy()
Expand All @@ -906,6 +919,8 @@ def rename_categories(self, new_categories, inplace=False):
if is_dict_like(new_categories):
cat.categories = [new_categories.get(item, item)
for item in cat.categories]
elif callable(new_categories):
cat.categories = [new_categories(item) for item in cat.categories]
else:
cat.categories = new_categories
if not inplace:
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ def test_rename_categories(self):

exp_cat = Index(["a", "b", "c"])
tm.assert_index_equal(cat.categories, exp_cat)
res = cat.rename_categories([1, 2, 3], inplace=True)

# GH18862 (let rename_categories take callables)
result = cat.rename_categories(lambda x: x.upper())
expected = Categorical(["A", "B", "C", "A"])
tm.assert_categorical_equal(result, expected)

# and now inplace
res = cat.rename_categories([1, 2, 3], inplace=True)
assert res is None
tm.assert_numpy_array_equal(cat.__array__(), np.array([1, 2, 3, 1],
dtype=np.int64))
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ def test_method_delegation(self):
tm.assert_index_equal(result, CategoricalIndex(
list('ffggef'), categories=list('efg')))

# GH18862 (let rename_categories take callables)
result = ci.rename_categories(lambda x: x.upper())
tm.assert_index_equal(result, CategoricalIndex(
list('AABBCA'), categories=list('CAB')))

ci = CategoricalIndex(list('aabbca'), categories=list('cab'))
result = ci.add_categories(['d'])
tm.assert_index_equal(result, CategoricalIndex(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ def f():
pytest.raises(Exception, f)
# right: s.cat.set_categories([4,3,2,1])

# GH18862 (let Series.cat.rename_categories take callables)
s = Series(Categorical(["a", "b", "c", "a"], ordered=True))
result = s.cat.rename_categories(lambda x: x.upper())
expected = Series(Categorical(["A", "B", "C", "A"],
categories=["A", "B", "C"],
ordered=True))
tm.assert_series_equal(result, expected)

def test_str_accessor_api_for_categorical(self):
# https://github.com/pandas-dev/pandas/issues/10661
from pandas.core.strings import StringMethods
Expand Down