Skip to content

Commit 5c8c57e

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 19ebee5 commit 5c8c57e

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
@@ -2226,7 +2226,7 @@ def groupby(self, to_groupby):
22262226

22272227
def map(self, mapper):
22282228
"""
2229-
Apply mapper function to its values.
2229+
Apply mapper function to an index
22302230
22312231
Parameters
22322232
----------
@@ -2235,9 +2235,10 @@ def map(self, mapper):
22352235
22362236
Returns
22372237
-------
2238-
applied : array
2238+
An Index reflecting an appropriate Index with the mapper
2239+
function applied
22392240
"""
2240-
return self._arrmap(self.values, mapper)
2241+
return self._shallow_copy_with_infer(self._arrmap(self.values, mapper))
22412242

22422243
def isin(self, values, level=None):
22432244
"""

pandas/tests/indexes/test_base.py

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

16161616
self.assertEqual(coerce(idx), expected)
16171617

1618+
def test_map(self):
1619+
1620+
# Applying a function to the Index
1621+
df = pd.DataFrame([[0, 1], [2, 3]], columns=['c1', 'c2'], index=['i1', 'i2'])
1622+
df.index.name = "Numbering"
1623+
df.index = df.index.map(lambda x: x.upper())
1624+
self.assertTrue(df.index.equals(Index(['I1', 'I2'])))
1625+
self.assertEqual(df.index.name, "Numbering")
1626+
1627+
testIdx = self.unicodeIndex.map(lambda x: len(x))
1628+
tm.assert_index_equal(testIdx, Int64Index([10]*100))
1629+
1630+
testIdx = self.strIndex.map(lambda x: len(x))
1631+
tm.assert_index_equal(testIdx, Int64Index([10]*100))
1632+
1633+
testIdx = self.dateIndex.map(lambda x: x + timedelta(days=1))
1634+
tm.assert_index_equal(
1635+
testIdx, DatetimeIndex([dt + timedelta(days=1) for dt in tm.makeDateIndex(100)]))
1636+
1637+
testIdx = self.periodIndex.map(lambda x: x.to_timestamp())
1638+
tm.assert_index_equal(testIdx, self.dateIndex)
1639+
1640+
testIdx = self.intIndex.map(lambda x: str(x))
1641+
tm.assert_index_equal(testIdx, Index([str(i) for i in range(100)]))
1642+
1643+
testIdx = self.floatIndex.map(lambda x: -1 if x < 0 else 1)
1644+
self.assertEqual(len(testIdx), 100)
1645+
self.assertTrue(isinstance(testIdx, Int64Index))
1646+
self.assertTrue(set(testIdx == {-1, 1}))
1647+
1648+
testIdx = self.boolIndex.map(lambda x: not x)
1649+
tm.assert_index_equal(testIdx, Index([False, True]))
1650+
1651+
testIdx = self.catIndex.map(lambda x: len(x))
1652+
16181653

16191654
def test_get_combined_index():
16201655
from pandas.core.index import _get_combined_index

0 commit comments

Comments
 (0)