Skip to content
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

API: map() on Index returns an Index, not array #12798

Closed
wants to merge 2 commits into from
Closed
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.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ New behaviour:

np.cumsum(sp, axis=0)

- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)

.. _whatsnew_0181.apply_resample:

Using ``.apply`` on groupby resampling
Expand Down
7 changes: 4 additions & 3 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ def groupby(self, values):

def map(self, mapper):
"""
Apply mapper function to its values.
Apply mapper function to an index

Parameters
----------
Expand All @@ -2436,9 +2436,10 @@ def map(self, mapper):

Returns
-------
applied : array
An Index reflecting an appropriate Index with the mapper
function applied
"""
return self._arrmap(self.values, mapper)
return self._shallow_copy_with_infer(self._arrmap(self.values, mapper))

def isin(self, values, level=None):
"""
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,41 @@ def test_fillna(self):
expected[1] = True
self.assert_numpy_array_equal(idx._isnan, expected)
self.assertTrue(idx.hasnans)

def test_map(self):
for name, index in self.indices.items():
if len(index) == 0 or isinstance(index, MultiIndex):
pass
else:
# Applying a function to the Index
index = index.map(lambda x: x)
print(name, index)
self.assertTrue(isinstance(index, Index))
#self.assertTrue(index.equals(Index(['I1', 'I2'])))
#self.assertEqual(index.name, "Numbering")
#
#testIdx = self.unicodeIndex.map(lambda x: len(x))
#tm.assert_index_equal(testIdx, Int64Index([10]*100))
#
#testIdx = self.strIndex.map(lambda x: len(x))
#tm.assert_index_equal(testIdx, Int64Index([10]*100))
#
#testIdx = self.dateIndex.map(lambda x: x + timedelta(days=1))
#tm.assert_index_equal(
# testIdx, DatetimeIndex([dt + timedelta(days=1) for dt in tm.makeDateIndex(100)]))
#
#testIdx = self.periodIndex.map(lambda x: x.to_timestamp())
#tm.assert_index_equal(testIdx, self.dateIndex)
#
#testIdx = self.intIndex.map(lambda x: str(x))
#tm.assert_index_equal(testIdx, Index([str(i) for i in range(100)]))
#
#testIdx = self.floatIndex.map(lambda x: -1 if x < 0 else 1)
#self.assertEqual(len(testIdx), 100)
#self.assertTrue(isinstance(testIdx, Int64Index))
#self.assertTrue(set(testIdx == {-1, 1}))
#
#testIdx = self.boolIndex.map(lambda x: not x)
#tm.assert_index_equal(testIdx, Index([False, True]))
#
#testIdx = self.catIndex.map(lambda x: len(x))
2 changes: 2 additions & 0 deletions pandas/tests/indexes/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,3 +1149,5 @@ def test_fillna_timedelta(self):
exp = pd.Index(
[pd.Timedelta('1 day'), 'x', pd.Timedelta('3 day')], dtype=object)
self.assert_index_equal(idx.fillna('x'), exp)