Skip to content

ENH: Allow dict as the argument to Index.map (GH13517) #13518

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ Other enhancements
- The ``pd.read_csv()`` with ``engine='python'`` has gained support for the ``na_filter`` option (:issue:`13321`)
- The ``pd.read_csv()`` with ``engine='python'`` has gained support for the ``memory_map`` option (:issue:`13381`)

- ``Index.map`` now accepts a dictionary as its argument (:issue:`13517`)
- ``Index.astype()`` now accepts an optional boolean argument ``copy``, which allows optional copying if the requirements on dtype are satisfied (:issue:`13209`)
- ``Index`` now supports the ``.where()`` function for same shape indexing (:issue:`13170`)

Expand Down
7 changes: 5 additions & 2 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2295,14 +2295,17 @@ def map(self, mapper):

Parameters
----------
mapper : callable
mapper : callable or dict
Function to be applied.

Returns
-------
applied : array
"""
return self._arrmap(self.values, mapper)
if isinstance(mapper, dict):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_dict_like covers a wider range of mappings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Fixed.

return self._arrmap(self.values, lambda key: mapper[key])
else:
return self._arrmap(self.values, mapper)

def isin(self, values, level=None):
"""
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Float64Index, Int64Index,
CategoricalIndex, DatetimeIndex, TimedeltaIndex,
PeriodIndex)
from pandas.util.testing import assert_almost_equal
from pandas.util.testing import assert_almost_equal, assert_numpy_array_equal
from pandas.compat.numpy import np_datetime64_compat

import pandas.core.config as cf
Expand Down Expand Up @@ -1462,6 +1462,16 @@ def test_conversion_preserves_name(self):
self.assertEqual(i.name, pd.to_datetime(i).name)
self.assertEqual(i.name, pd.to_timedelta(i).name)

def test_map_dict(self):
# GH 13517
i = pd.Index(['a', 'b', 'c', 'd'])
d = {'a': 0, 'b': 2, 'c': 1, 'd': 5}

actual = i.map(d)
expected = np.array([0, 2, 1, 5])

tm.assert_numpy_array_equal(actual, expected)

def test_string_index_repr(self):
# py3/py2 repr can differ because of "u" prefix
# which also affects to displayed element size
Expand Down