Skip to content

Commit 5849db5

Browse files
committed
API: map() on Index returns an Index, not array
[ENH] Using _shallow_copy_with_infer, additional test for type change and attribute conservation TST: Tests for various index types REF: Move to tm.assert_index_equal DOC: docstring for the map function
1 parent c31ea34 commit 5849db5

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

doc/source/whatsnew/v0.18.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ New behaviour:
408408

409409
np.cumsum(sp, axis=0)
410410

411+
- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)
412+
411413
.. _whatsnew_0181.apply_resample:
412414

413415
Using ``.apply`` on groupby resampling

pandas/indexes/base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,7 @@ def groupby(self, values):
24272427

24282428
def map(self, mapper):
24292429
"""
2430-
Apply mapper function to its values.
2430+
Apply mapper function to an index
24312431
24322432
Parameters
24332433
----------
@@ -2436,9 +2436,10 @@ def map(self, mapper):
24362436
24372437
Returns
24382438
-------
2439-
applied : array
2439+
An Index reflecting an appropriate Index with the mapper
2440+
function applied
24402441
"""
2441-
return self._arrmap(self.values, mapper)
2442+
return self._shallow_copy_with_infer(self._arrmap(self.values, mapper))
24422443

24432444
def isin(self, values, level=None):
24442445
"""

pandas/tests/indexes/test_base.py

+35
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,41 @@ def test_string_index_repr(self):
17441744

17451745
self.assertEqual(coerce(idx), expected)
17461746

1747+
def test_map(self):
1748+
1749+
# Applying a function to the Index
1750+
df = pd.DataFrame([[0, 1], [2, 3]], columns=['c1', 'c2'], index=['i1', 'i2'])
1751+
df.index.name = "Numbering"
1752+
df.index = df.index.map(lambda x: x.upper())
1753+
self.assertTrue(df.index.equals(Index(['I1', 'I2'])))
1754+
self.assertEqual(df.index.name, "Numbering")
1755+
1756+
testIdx = self.unicodeIndex.map(lambda x: len(x))
1757+
tm.assert_index_equal(testIdx, Int64Index([10]*100))
1758+
1759+
testIdx = self.strIndex.map(lambda x: len(x))
1760+
tm.assert_index_equal(testIdx, Int64Index([10]*100))
1761+
1762+
testIdx = self.dateIndex.map(lambda x: x + timedelta(days=1))
1763+
tm.assert_index_equal(
1764+
testIdx, DatetimeIndex([dt + timedelta(days=1) for dt in tm.makeDateIndex(100)]))
1765+
1766+
testIdx = self.periodIndex.map(lambda x: x.to_timestamp())
1767+
tm.assert_index_equal(testIdx, self.dateIndex)
1768+
1769+
testIdx = self.intIndex.map(lambda x: str(x))
1770+
tm.assert_index_equal(testIdx, Index([str(i) for i in range(100)]))
1771+
1772+
testIdx = self.floatIndex.map(lambda x: -1 if x < 0 else 1)
1773+
self.assertEqual(len(testIdx), 100)
1774+
self.assertTrue(isinstance(testIdx, Int64Index))
1775+
self.assertTrue(set(testIdx == {-1, 1}))
1776+
1777+
testIdx = self.boolIndex.map(lambda x: not x)
1778+
tm.assert_index_equal(testIdx, Index([False, True]))
1779+
1780+
testIdx = self.catIndex.map(lambda x: len(x))
1781+
17471782

17481783
class TestMixedIntIndex(Base, tm.TestCase):
17491784
# Mostly the tests from common.py for which the results differ

0 commit comments

Comments
 (0)