Skip to content

Commit 7f0e35e

Browse files
committed
fix uints
1 parent d01d68b commit 7f0e35e

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

pandas/core/indexes/base.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,7 @@ def map(self, mapper, na_action=None):
29102910
from .multi import MultiIndex
29112911
new_values = super(Index, self)._map_values(
29122912
mapper, na_action=na_action)
2913+
29132914
attributes = self._get_attributes_dict()
29142915
if new_values.size and isinstance(new_values[0], tuple):
29152916
if isinstance(self, MultiIndex):
@@ -2923,8 +2924,14 @@ def map(self, mapper, na_action=None):
29232924

29242925
attributes['copy'] = False
29252926

2926-
# we infer the result types based on the
2927-
# returned values
2927+
# we want to try to return our original dtype
2928+
# ints infer to integer, but if we have
2929+
# uints, would prefer to return these
2930+
if is_unsigned_integer_dtype(self.dtype):
2931+
inferred = lib.infer_dtype(new_values)
2932+
if inferred == 'integer':
2933+
attributes['dtype'] = self.dtype
2934+
29282935
return Index(new_values, **attributes)
29292936

29302937
def isin(self, values, level=None):

pandas/tests/indexes/common.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -1007,16 +1007,11 @@ def test_searchsorted_monotonic(self, indices):
10071007
indices._searchsorted_monotonic(value, side='left')
10081008

10091009
def test_map(self):
1010+
# callable
10101011
index = self.create_index()
1011-
1012-
# From output of UInt64Index mapping can't infer that we
1013-
# shouldn't default to Int64
1014-
if isinstance(index, UInt64Index):
1015-
expected = Index(index.values.tolist())
1016-
else:
1017-
expected = index
1018-
1019-
tm.assert_index_equal(index.map(lambda x: x), expected)
1012+
expected = index
1013+
result = index.map(lambda x: x)
1014+
tm.assert_index_equal(result, expected)
10201015

10211016
@pytest.mark.parametrize(
10221017
"mapper",
@@ -1028,13 +1023,7 @@ def test_map_dictlike(self, mapper):
10281023
index = self.create_index()
10291024
if isinstance(index, pd.CategoricalIndex):
10301025
pytest.skip("tested in test_categorical")
1031-
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
1026+
expected = index
10381027

10391028
identity = mapper({x: x for x in index}, index)
10401029
result = index.map(identity)

pandas/tests/indexes/test_base.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,15 @@ def test_map_tseries_indices_return_index(self):
852852
exp = Index(range(24), name='hourly')
853853
tm.assert_index_equal(exp, date_index.map(lambda x: x.hour))
854854

855-
def test_map_with_dict_and_series(self):
855+
@pytest.mark.parametrize(
856+
"mapper",
857+
[
858+
lambda values, index: {i: e for e, i in zip(values, index)},
859+
lambda values, index: pd.Series(values, index)])
860+
def test_map_dictlike(self, mapper):
856861
# GH 12756
857862
expected = Index(['foo', 'bar', 'baz'])
858-
mapper = Series(expected.values, index=[0, 1, 2])
859-
result = tm.makeIntIndex(3).map(mapper)
863+
result = tm.makeIntIndex(3).map(mapper(expected.values, [0, 1, 2]))
860864
tm.assert_index_equal(result, expected)
861865

862866
for name in self.indices.keys():
@@ -867,21 +871,16 @@ def test_map_with_dict_and_series(self):
867871
# Cannot map duplicated index
868872
continue
869873

870-
cur_index = self.indices[name]
871-
expected = Index(np.arange(len(cur_index), 0, -1))
872-
mapper = pd.Series(expected, index=cur_index)
873-
result = cur_index.map(mapper)
874-
875-
tm.assert_index_equal(result, expected)
876-
877-
# If the mapper is empty the expected index type is Int64Index
878-
# but the output defaults to Float64 so I treat it independently
879-
mapper = {o: n for o, n in
880-
zip(cur_index, expected)}
874+
index = self.indices[name]
875+
expected = Index(np.arange(len(index), 0, -1))
881876

882-
result = cur_index.map(mapper)
883-
if not mapper:
877+
# to match proper result coercion for uints
878+
if name == 'uintIndex':
879+
expected = expected.astype('uint64')
880+
elif name == 'empty':
884881
expected = Float64Index([])
882+
883+
result = index.map(mapper(expected, index))
885884
tm.assert_index_equal(result, expected)
886885

887886
def test_map_with_non_function_missing_values(self):

0 commit comments

Comments
 (0)