Skip to content

Commit a110be9

Browse files
committed
make map on time tseries indices return index if dtype of output is not a tseries; sphinx changes; fix docstring
1 parent a596744 commit a110be9

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

doc/source/whatsnew/v0.20.0.txt

+11-11
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,26 @@ Backwards incompatible API changes
9292
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9393

9494
- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)
95-
.. ipython:: python
9695

97-
idx = Index([1, 2])
98-
idx
96+
.. ipython:: python
9997

100-
Previous Behavior:
98+
idx = Index([1, 2])
99+
idx
101100

102-
.. code-block:: ipython
101+
Previous Behavior:
103102

104-
In [3]: idx.map(lambda x: x * 2)
105-
Out[3]: array([2, 4])
103+
.. code-block:: ipython
106104

107-
New Behavior:
105+
In [3]: idx.map(lambda x: x * 2)
106+
Out[3]: array([2, 4])
108107

109-
.. ipython:: python
108+
New Behavior:
110109

111-
idx.map(lambda x: x * 2)
110+
.. ipython:: python
112111

113-
.. _whatsnew_0200.api:
112+
idx.map(lambda x: x * 2)
114113

114+
.. _whatsnew_0200.api:
115115

116116
- ``CParserError`` has been renamed to ``ParserError`` in ``pd.read_csv`` and will be removed in the future (:issue:`12665`)
117117
- ``SparseArray.cumsum()`` and ``SparseSeries.cumsum()`` will now always return ``SparseArray`` and ``SparseSeries`` respectively (:issue:`12855`)

pandas/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,8 @@ def map(self, mapper):
24392439
-------
24402440
applied : Index
24412441
The output of the mapping function applied to the index.
2442-
If the function returns a tuple a
2442+
If the function returns a tuple with more than one element
2443+
a MultiIndex will be returned.
24432444
"""
24442445
from .multi import MultiIndex
24452446
mapped_values = self._arrmap(self.values, mapper)

pandas/tests/indexes/test_base.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -772,25 +772,43 @@ def test_map_identity_mapping(self):
772772
for name, cur_index in self.indices.items():
773773
self.assert_index_equal(cur_index, cur_index.map(lambda x: x))
774774

775-
def test_map_that_returns_tuples_creates_multi_index(self):
775+
def test_map_with_tuples(self):
776776
# GH 12766
777-
boolean_index = tm.makeIntIndex(3).map(lambda x: (x, x == 1))
778-
expected = MultiIndex.from_tuples([(0, False), (1, True), (2, False)])
777+
778+
# Test that returning a single tuple from an Index
779+
# returns an Index.
780+
boolean_index = tm.makeIntIndex(3).map(lambda x: (x,))
781+
expected = Index([(0,), (1,), (2,)])
779782
self.assert_index_equal(boolean_index, expected)
780783

781-
def test_map_that_returns_a_length_one_tuple_creates_an_index(self):
782-
# GH 12766
783-
boolean_index = tm.makeIntIndex(3).map(lambda x: (x, ))
784-
expected = Index([(0, ), (1, ), (2, )])
784+
# Test that returning a tuple from a map of a single index
785+
# returns a MultiIndex object.
786+
boolean_index = tm.makeIntIndex(3).map(lambda x: (x, x == 1))
787+
expected = MultiIndex.from_tuples([(0, False), (1, True), (2, False)])
785788
self.assert_index_equal(boolean_index, expected)
786789

787-
def test_map_that_reduces_multi_index_to_single_index_returns_index(self):
788-
# GH 12766
790+
# Test that returning a single object from a MultiIndex
791+
# returns an Index.
789792
first_level = ['foo', 'bar', 'baz']
790793
multi_index = MultiIndex.from_tuples(lzip(first_level, [1, 2, 3]))
791794
reduced_index = multi_index.map(lambda x: x[0])
792795
self.assert_index_equal(reduced_index, Index(first_level))
793796

797+
def test_map_tseries_indices_return_index(self):
798+
date_index = tm.makeDateIndex(10)
799+
exp = Index([1] * 10)
800+
self.assert_index_equal(exp, date_index.map(lambda x: 1))
801+
802+
period_index = tm.makePeriodIndex(10)
803+
self.assert_index_equal(exp, period_index.map(lambda x: 1))
804+
805+
tdelta_index = tm.makeTimedeltaIndex(10)
806+
self.assert_index_equal(exp, tdelta_index.map(lambda x: 1))
807+
808+
date_index = tm.makeDateIndex(24, freq='h', name='hourly')
809+
exp = Index(range(24), name='hourly')
810+
self.assert_index_equal(exp, date_index.map(lambda x: x.hour))
811+
794812
def test_append_multiple(self):
795813
index = Index(['a', 'b', 'c', 'd', 'e', 'f'])
796814

pandas/tseries/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ def _nat_new(self, box=True):
330330
def map(self, f):
331331
try:
332332
result = f(self)
333-
if not isinstance(result, (np.ndarray, Index)):
333+
if not isinstance(result, Index):
334334
raise TypeError
335335
return result
336336
except Exception:
337-
return _algos.arrmap_object(self.asobject.values, f)
337+
return self.asobject.map(f)
338338

339339
def sort_values(self, return_indexer=False, ascending=True):
340340
"""

0 commit comments

Comments
 (0)