Skip to content

Commit b23055a

Browse files
committed
Makes changes based on feedback from @jreback
1 parent c7d9eb2 commit b23055a

File tree

9 files changed

+65
-104
lines changed

9 files changed

+65
-104
lines changed

pandas/core/indexes/base.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,23 +2820,25 @@ def get_indexer_for(self, target, **kwargs):
28202820
indexer, _ = self.get_indexer_non_unique(target, **kwargs)
28212821
return indexer
28222822

2823-
def get_values_from_dict(self, input_dict):
2824-
"""Return the values of the input dictionary in the order the keys are
2823+
_index_shared_docs['_get_values_from_dict'] = """
2824+
Return the values of the input dictionary in the order the keys are
28252825
in the index. np.nan is returned for index values not in the
28262826
dictionary.
28272827
28282828
Parameters
28292829
----------
2830-
input_dict : dict
2830+
data : dict
28312831
The dictionary from which to extract the values
28322832
28332833
Returns
28342834
-------
2835-
Union[np.array, list]
2835+
np.array
28362836
28372837
"""
28382838

2839-
return lib.fast_multiget(input_dict, self.values,
2839+
@Appender(_index_shared_docs['_get_values_from_dict'])
2840+
def _get_values_from_dict(self, data):
2841+
return lib.fast_multiget(data, self.values,
28402842
default=np.nan)
28412843

28422844
def _maybe_promote(self, other):
@@ -2882,8 +2884,9 @@ def map(self, mapper):
28822884
28832885
Parameters
28842886
----------
2885-
mapper : Union[function, dict, Series]
2887+
mapper : {callable, dict, Series}
28862888
Function to be applied or input correspondence object.
2889+
dict and Series support new in 0.20.0.
28872890
28882891
Returns
28892892
-------
@@ -2900,7 +2903,7 @@ def map(self, mapper):
29002903
mapped_values = algos.take_1d(mapper.values, indexer)
29012904
elif isinstance(mapper, dict):
29022905
idx = Index(mapper.keys())
2903-
data = idx.get_values_from_dict(mapper)
2906+
data = idx._get_values_from_dict(mapper)
29042907
indexer = idx.get_indexer(self.values)
29052908
mapped_values = algos.take_1d(data, indexer)
29062909
else:

pandas/core/indexes/datetimelike.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,14 @@ def __rsub__(self, other):
697697
def _add_delta(self, other):
698698
return NotImplemented
699699

700+
@Appender(_index_shared_docs['_get_values_from_dict'])
701+
def _get_values_from_dict(self, data):
702+
if len(data):
703+
return np.array([data.get(i, np.nan)
704+
for i in self.asobject.values])
705+
706+
return np.array([np.nan])
707+
700708
def _add_delta_td(self, other):
701709
# add a delta of a timedeltalike
702710
# return the i8 result view

pandas/core/indexes/datetimes.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,29 +1409,16 @@ def get_value_maybe_box(self, series, key):
14091409
key, tz=self.tz)
14101410
return _maybe_box(self, values, series, key)
14111411

1412-
def get_values_from_dict(self, input_dict):
1413-
"""Return the values of the input dictionary in the order the keys are
1414-
in the index. np.nan is returned for index values not in the
1415-
dictionary.
1416-
1417-
Parameters
1418-
----------
1419-
input_dict : dict
1420-
The dictionary from which to extract the values
1421-
1422-
Returns
1423-
-------
1424-
Union[np.array, list]
1425-
1426-
"""
1427-
if len(input_dict):
1412+
@Appender(_index_shared_docs['_get_values_from_dict'])
1413+
def _get_values_from_dict(self, data):
1414+
if len(data):
14281415
# coerce back to datetime objects for lookup
1429-
input_dict = com._dict_compat(input_dict)
1430-
return lib.fast_multiget(input_dict,
1416+
data = com._dict_compat(data)
1417+
return lib.fast_multiget(data,
14311418
self.asobject.values,
14321419
default=np.nan)
1433-
else:
1434-
return np.nan
1420+
1421+
return np.array([np.nan])
14351422

14361423
def get_loc(self, key, method=None, tolerance=None):
14371424
"""

pandas/core/indexes/period.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -800,25 +800,6 @@ def _get_unique_index(self, dropna=False):
800800
res = res.dropna()
801801
return res
802802

803-
def get_values_from_dict(self, input_dict):
804-
"""Return the values of the input dictionary in the order the keys are
805-
in the index. np.nan is returned for index values not in the
806-
dictionary.
807-
808-
Parameters
809-
----------
810-
input_dict : dict
811-
The dictionary from which to extract the values
812-
813-
Returns
814-
-------
815-
Union[np.array, list]
816-
817-
"""
818-
819-
return np.array([input_dict.get(i, np.nan) for i in self.values]
820-
if input_dict else [np.nan])
821-
822803
def get_loc(self, key, method=None, tolerance=None):
823804
"""
824805
Get integer location for requested label

pandas/core/indexes/timedeltas.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -680,26 +680,6 @@ def get_value_maybe_box(self, series, key):
680680
values = self._engine.get_value(_values_from_object(series), key)
681681
return _maybe_box(self, values, series, key)
682682

683-
def get_values_from_dict(self, input_dict):
684-
"""Return the values of the input dictionary in the order the keys are
685-
in the index. np.nan is returned for index values not in the
686-
dictionary.
687-
688-
Parameters
689-
----------
690-
input_dict : dict
691-
The dictionary from which to extract the values
692-
693-
Returns
694-
-------
695-
Union[np.array, list]
696-
697-
"""
698-
699-
return np.array([input_dict.get(i, np.nan)
700-
for i in self.asobject.values]
701-
if input_dict else [np.nan])
702-
703683
def get_loc(self, key, method=None, tolerance=None):
704684
"""
705685
Get integer location for requested label

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
204204
index = Index(_try_sort(data))
205205

206206
try:
207-
data = index.get_values_from_dict(data)
207+
data = index._get_values_from_dict(data)
208208
except TypeError:
209209
data = ([data.get(i, np.nan) for i in index]
210210
if data else np.nan)

pandas/tests/indexes/test_base.py

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -830,60 +830,32 @@ def test_map_tseries_indices_return_index(self):
830830
tm.assert_index_equal(exp, date_index.map(lambda x: x.hour))
831831

832832
def test_map_with_dict_and_series(self):
833+
# GH 12756
833834
expected = Index(['foo', 'bar', 'baz'])
834835
mapper = Series(expected.values, index=[0, 1, 2])
835-
tm.assert_index_equal(tm.makeIntIndex(3).map(mapper), expected)
836-
837-
# GH 12766
838-
# special = []
839-
special = ['catIndex']
840-
841-
for name in special:
842-
orig_values = ['a', 'B', 1, 'a']
843-
new_values = ['one', 2, 3.0, 'one']
844-
cur_index = CategoricalIndex(orig_values, name='XXX')
845-
expected = CategoricalIndex(new_values,
846-
name='XXX', categories=[3.0, 2, 'one'])
847-
848-
mapper = pd.Series(new_values[:-1], index=orig_values[:-1])
849-
output = cur_index.map(mapper)
850-
# Order of categories in output can be different
851-
tm.assert_index_equal(expected, output)
836+
result = tm.makeIntIndex(3).map(mapper)
837+
tm.assert_index_equal(result, expected)
852838

853-
mapper = {o: n for o, n in
854-
zip(orig_values[:-1], new_values[:-1])}
855-
output = cur_index.map(mapper)
856-
# Order of categories in output can be different
857-
tm.assert_index_equal(expected, output)
839+
for name in self.indices.keys():
840+
if name == 'catIndex':
841+
# Tested in test_categorical
842+
continue
858843

859-
for name in list(set(self.indices.keys()) - set(special)):
860844
cur_index = self.indices[name]
861845
expected = Index(np.arange(len(cur_index), 0, -1))
862846
mapper = pd.Series(expected, index=cur_index)
863847
tm.assert_index_equal(expected, cur_index.map(mapper))
864848

865849
mapper = {o: n for o, n in
866850
zip(cur_index, expected)}
851+
# If the mapper is empty the expected index type is Int64Index
852+
# but the output defaults to Float64 so I treat it independently
867853
if mapper:
868854
tm.assert_index_equal(expected, cur_index.map(mapper))
869855
else:
870-
# The expected index type is Int64Index
871-
# but the output defaults to Float64
872856
tm.assert_index_equal(Float64Index([]),
873857
cur_index.map(mapper))
874858

875-
def test_map_with_categorical_series(self):
876-
# GH 12756
877-
a = Index([1, 2, 3, 4])
878-
b = Series(["even", "odd", "even", "odd"],
879-
dtype="category")
880-
c = Series(["even", "odd", "even", "odd"])
881-
882-
exp = CategoricalIndex(["odd", "even", "odd", np.nan])
883-
tm.assert_index_equal(a.map(b), exp)
884-
exp = Index(["odd", "even", "odd", np.nan])
885-
tm.assert_index_equal(a.map(c), exp)
886-
887859
def test_map_with_non_function_missing_values(self):
888860
# GH 12756
889861
expected = Index([2., np.nan, 'foo'])

pandas/tests/indexes/test_category.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ def f(x):
250250
result = ci.map({'A': 10, 'B': 20, 'C': 30})
251251
tm.assert_index_equal(result, exp)
252252

253+
def test_map_with_categorical_series(self):
254+
# GH 12756
255+
a = pd.Index([1, 2, 3, 4])
256+
b = pd.Series(["even", "odd", "even", "odd"],
257+
dtype="category")
258+
c = pd.Series(["even", "odd", "even", "odd"])
259+
260+
exp = CategoricalIndex(["odd", "even", "odd", np.nan])
261+
tm.assert_index_equal(a.map(b), exp)
262+
exp = pd.Index(["odd", "even", "odd", np.nan])
263+
tm.assert_index_equal(a.map(c), exp)
264+
253265
def test_where(self):
254266
i = self.create_index()
255267
result = i.where(notna(i))

pandas/tests/indexing/test_categorical.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,21 @@ def test_indexing_with_category(self):
439439

440440
res = (cat[['A']] == 'foo')
441441
tm.assert_frame_equal(res, exp)
442+
443+
def test_map_with_dict_or_series(self):
444+
orig_values = ['a', 'B', 1, 'a']
445+
new_values = ['one', 2, 3.0, 'one']
446+
cur_index = pd.CategoricalIndex(orig_values, name='XXX')
447+
expected = pd.CategoricalIndex(new_values,
448+
name='XXX', categories=[3.0, 2, 'one'])
449+
450+
mapper = pd.Series(new_values[:-1], index=orig_values[:-1])
451+
output = cur_index.map(mapper)
452+
# Order of categories in output can be different
453+
tm.assert_index_equal(expected, output)
454+
455+
mapper = {o: n for o, n in
456+
zip(orig_values[:-1], new_values[:-1])}
457+
output = cur_index.map(mapper)
458+
# Order of categories in output can be different
459+
tm.assert_index_equal(expected, output)

0 commit comments

Comments
 (0)