Skip to content

Commit d353f38

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 3dc0056 commit d353f38

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
@@ -39,26 +39,26 @@ Backwards incompatible API changes
3939
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4040

4141
- ``map`` on an ``Index`` now returns an ``Index``, not an array (:issue:`12766`)
42-
.. ipython:: python
4342

44-
idx = Index([1, 2])
45-
idx
43+
.. ipython:: python
4644

47-
Previous Behavior:
45+
idx = Index([1, 2])
46+
idx
4847

49-
.. code-block:: ipython
48+
Previous Behavior:
5049

51-
In [3]: idx.map(lambda x: x * 2)
52-
Out[3]: array([2, 4])
50+
.. code-block:: ipython
5351

54-
New Behavior:
52+
In [3]: idx.map(lambda x: x * 2)
53+
Out[3]: array([2, 4])
5554

56-
.. ipython:: python
55+
New Behavior:
5756

58-
idx.map(lambda x: x * 2)
57+
.. ipython:: python
5958

60-
.. _whatsnew_0200.api:
59+
idx.map(lambda x: x * 2)
6160

61+
.. _whatsnew_0200.api:
6262

6363

6464

pandas/indexes/base.py

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

pandas/tests/indexes/test_base.py

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

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

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

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

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

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)