Skip to content

Commit e58cca2

Browse files
committed
Address comments from @jreback add new tests; skip tests on IntervalIndex
1 parent 828a48f commit e58cca2

File tree

8 files changed

+86
-28
lines changed

8 files changed

+86
-28
lines changed

pandas/core/base.py

+21
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,27 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
898898
return func(**kwds)
899899

900900
def _map_values(self, values, arg, na_action=None):
901+
"""An internal function that maps values using the input
902+
correspondence (which can be a dict, Series, or function).
903+
904+
Parameters
905+
----------
906+
values : np.ndarray
907+
The values to be mapped
908+
arg : function, dict, or Series
909+
The input correspondence object
910+
na_action : {None, 'ignore'}
911+
If 'ignore', propagate NA values, without passing them to the
912+
mapping function
913+
914+
Returns
915+
-------
916+
applied : {Index, MultiIndex}, inferred
917+
The output of the mapping function applied to the index.
918+
If the function returns a tuple with more than one element
919+
a MultiIndex will be returned.
920+
921+
"""
901922
if is_extension_type(self.dtype):
902923
if na_action is not None:
903924
raise NotImplementedError

pandas/core/indexes/base.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2675,7 +2675,7 @@ def get_indexer_for(self, target, **kwargs):
26752675
26762676
Parameters
26772677
----------
2678-
data : {dict, DictWithoutMissing}
2678+
data : dict
26792679
The dictionary from which to extract the values
26802680
26812681
Returns
@@ -2752,8 +2752,14 @@ def map(self, arg, na_action=None):
27522752
self.values, arg, na_action=na_action)
27532753
attributes = self._get_attributes_dict()
27542754
if new_values.size and isinstance(new_values[0], tuple):
2755+
if isinstance(self, MultiIndex):
2756+
names = self.names
2757+
elif attributes.get('name'):
2758+
names = [attributes.get('name')] * len(new_values[0])
2759+
else:
2760+
names = None
27552761
return MultiIndex.from_tuples(new_values,
2756-
names=attributes.get('name'))
2762+
names=names)
27572763

27582764
attributes['copy'] = False
27592765
return Index(new_values, **attributes)

pandas/tests/indexes/common.py

+26
Original file line numberDiff line numberDiff line change
@@ -937,3 +937,29 @@ def test_empty(self):
937937
index = self.create_index()
938938
assert not index.empty
939939
assert index[:0].empty
940+
941+
def test_map(self):
942+
index = self.create_index()
943+
# From output of UInt64Index mapping can't infer that we
944+
# shouldn't default to Int64
945+
if isinstance(index, UInt64Index):
946+
expected = Index(index.values.tolist())
947+
else:
948+
expected = index
949+
950+
tm.assert_index_equal(index.map(lambda x: x), expected)
951+
952+
identity_dict = {x: x for x in index}
953+
tm.assert_index_equal(index.map(identity_dict), expected)
954+
955+
# Use values to work around MultiIndex instantiation of series
956+
identity_series = Series(expected.values, index=index)
957+
tm.assert_index_equal(index.map(identity_series), expected)
958+
959+
# empty mappable
960+
nan_index = pd.Index([np.nan] * len(index))
961+
series_map = pd.Series()
962+
tm.assert_index_equal(index.map(series_map), nan_index)
963+
964+
dict_map = {}
965+
tm.assert_index_equal(index.map(dict_map), nan_index)

pandas/tests/indexes/datetimelike.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
""" generic datetimelike tests """
2-
2+
import pandas as pd
33
from .common import Base
44
import pandas.util.testing as tm
55

@@ -38,3 +38,24 @@ def test_view(self):
3838
i_view = i.view(self._holder)
3939
result = self._holder(i)
4040
tm.assert_index_equal(result, i_view)
41+
42+
def test_map(self):
43+
expected = self.index + 1
44+
tm.assert_index_equal(self.index.map(lambda x: x + 1), expected)
45+
46+
series_map = pd.Series(expected, self.index)
47+
tm.assert_index_equal(self.index.map(series_map), expected)
48+
49+
dict_map = {i: e for e, i in zip(expected, self.index)}
50+
tm.assert_index_equal(self.index.map(dict_map), expected)
51+
52+
# map to NaT
53+
result = self.index.map(lambda x: pd.NaT if x == self.index[0] else x)
54+
expected = pd.Index([pd.NaT] + self.index[1:].tolist())
55+
tm.assert_index_equal(result, expected)
56+
57+
series_map = pd.Series(expected, self.index)
58+
tm.assert_index_equal(self.index.map(series_map), expected)
59+
60+
dict_map = {i: e for e, i in zip(expected, self.index)}
61+
tm.assert_index_equal(self.index.map(dict_map), expected)

pandas/tests/indexes/datetimes/test_datetimelike.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,4 @@ def test_union(self):
7676
assert tm.equalContents(result, everything)
7777

7878
def test_map(self):
79-
expected = self.index + 1
80-
tm.assert_index_equal(self.index.map(lambda x: x + 1), expected)
81-
82-
series_map = pd.Series(expected, self.index)
83-
tm.assert_index_equal(self.index.map(series_map), expected)
84-
85-
dict_map = {i: e for e, i in zip(expected, self.index)}
86-
tm.assert_index_equal(self.index.map(dict_map), expected)
87-
88-
# empty mappable
89-
nan_index = Index([pd.np.nan] * len(self.index))
90-
series_map = pd.Series()
91-
tm.assert_index_equal(self.index.map(series_map), nan_index)
92-
dict_map = {}
93-
tm.assert_index_equal(self.index.map(dict_map), nan_index)
94-
95-
# map to NaT
96-
result = self.index.map(lambda x: pd.NaT if x == self.index[0] else x)
97-
expected = Index([pd.NaT] + self.index[1:].tolist())
98-
tm.assert_index_equal(result, expected)
99-
series_map = pd.Series(expected, self.index)
100-
tm.assert_index_equal(self.index.map(series_map), expected)
101-
dict_map = {i: e for e, i in zip(expected, self.index)}
102-
tm.assert_index_equal(self.index.map(dict_map), expected)
79+
super(TestDatetimeIndex, self).test_map()

pandas/tests/indexes/period/test_period.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,9 @@ def test_pickle_freq(self):
764764
assert new_prng.freq == offsets.MonthEnd()
765765
assert new_prng.freqstr == 'M'
766766

767-
def test_map_with_ordinal(self):
767+
def test_map(self):
768+
super(TestPeriodIndex, self).test_map()
769+
768770
index = PeriodIndex([2005, 2007, 2009], freq='A')
769771
result = index.map(lambda x: x.ordinal)
770772
exp = Index([x.ordinal for x in index])

pandas/tests/indexes/test_interval.py

+4
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ def test_repr_max_seq_item_setting(self):
306306
def test_repr_roundtrip(self):
307307
super(TestIntervalIndex, self).test_repr_roundtrip()
308308

309+
@pytest.mark.xfail(reason='get_indexer behavior does not currently work')
310+
def test_map(self):
311+
super(TestIntervalIndex, self).test_map()
312+
309313
def test_get_item(self):
310314
i = IntervalIndex.from_arrays((0, 1, np.nan), (1, 2, np.nan),
311315
closed='right')

pandas/tests/indexes/timedeltas/test_timedelta.py

+1
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def test_misc_coverage(self):
353353
assert not idx.equals(list(non_td))
354354

355355
def test_map(self):
356+
super(TestTimedeltaIndex, self).test_map()
356357

357358
rng = timedelta_range('1 day', periods=10)
358359

0 commit comments

Comments
 (0)