Skip to content

Commit 4dfe39a

Browse files
committed
TST: test_map parametrize
xref pandas-dev#18482
1 parent 38f41e6 commit 4dfe39a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

pandas/tests/indexes/common.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -1018,20 +1018,32 @@ def test_map(self):
10181018

10191019
tm.assert_index_equal(index.map(lambda x: x), expected)
10201020

1021-
identity_dict = {x: x for x in index}
1022-
tm.assert_index_equal(index.map(identity_dict), expected)
1021+
@pytest.mark.parametrize(
1022+
"mapper",
1023+
[
1024+
lambda values, index: {i: e for e, i in zip(values, index)},
1025+
lambda values, index: pd.Series(values, index)])
1026+
def test_map_dictlike(self, mapper):
10231027

1024-
# Use values to work around MultiIndex instantiation of series
1025-
identity_series = Series(expected.values, index=index)
1026-
tm.assert_index_equal(index.map(identity_series), expected)
1028+
index = self.create_index()
1029+
if isinstance(index, pd.CategoricalIndex):
1030+
pytest.skip("tested in test_categorical")
10271031

1028-
# empty mappable
1029-
nan_index = pd.Index([np.nan] * len(index))
1030-
series_map = pd.Series()
1031-
tm.assert_index_equal(index.map(series_map), nan_index)
1032+
# From output of UInt64Index mapping can't infer that we
1033+
# shouldn't default to Int64
1034+
if isinstance(index, UInt64Index):
1035+
expected = Index(index.values.tolist())
1036+
else:
1037+
expected = index
1038+
1039+
identity = mapper({x: x for x in index}, index)
1040+
result = index.map(identity)
1041+
tm.assert_index_equal(result, expected)
10321042

1033-
dict_map = {}
1034-
tm.assert_index_equal(index.map(dict_map), nan_index)
1043+
# empty mappable
1044+
expected = pd.Index([np.nan] * len(index))
1045+
result = index.map(mapper(expected, index))
1046+
tm.assert_index_equal(result, expected)
10351047

10361048
def test_putmask_with_wrong_mask(self):
10371049
# GH18368

0 commit comments

Comments
 (0)