Skip to content

Commit 0031198

Browse files
oguzhanogredenproost
authored andcommitted
Correct type inference for UInt64Index during access (pandas-dev#29420)
1 parent 2e78fd9 commit 0031198

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

doc/source/whatsnew/v1.0.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ Numeric
534534
- Improved error message when using `frac` > 1 and `replace` = False (:issue:`27451`)
535535
- Bug in numeric indexes resulted in it being possible to instantiate an :class:`Int64Index`, :class:`UInt64Index`, or :class:`Float64Index` with an invalid dtype (e.g. datetime-like) (:issue:`29539`)
536536
- Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`)
537-
-
537+
- Bug in :class:`NumericIndex` construction that caused indexing to fail when integers in the ``np.uint64`` range were used (:issue:`28023`)
538+
- Bug in :class:`NumericIndex` construction that caused :class:`UInt64Index` to be casted to :class:`Float64Index` when integers in the ``np.uint64`` range were used to index a :class:`DataFrame` (:issue:`28279`)
538539

539540
Conversion
540541
^^^^^^^^^^

pandas/core/indexes/numeric.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

3-
from pandas._libs import index as libindex
3+
from pandas._libs import index as libindex, lib
44
from pandas.util._decorators import Appender, cache_readonly
55

66
from pandas.core.dtypes.cast import astype_nansafe
@@ -320,13 +320,15 @@ def _convert_scalar_indexer(self, key, kind=None):
320320

321321
@Appender(_index_shared_docs["_convert_arr_indexer"])
322322
def _convert_arr_indexer(self, keyarr):
323-
# Cast the indexer to uint64 if possible so
324-
# that the values returned from indexing are
325-
# also uint64.
326-
keyarr = com.asarray_tuplesafe(keyarr)
327-
if is_integer_dtype(keyarr):
328-
return com.asarray_tuplesafe(keyarr, dtype=np.uint64)
329-
return keyarr
323+
# Cast the indexer to uint64 if possible so that the values returned
324+
# from indexing are also uint64.
325+
dtype = None
326+
if is_integer_dtype(keyarr) or (
327+
lib.infer_dtype(keyarr, skipna=False) == "integer"
328+
):
329+
dtype = np.uint64
330+
331+
return com.asarray_tuplesafe(keyarr, dtype=dtype)
330332

331333
@Appender(_index_shared_docs["_convert_index_indexer"])
332334
def _convert_index_indexer(self, keyarr):

pandas/tests/indexes/test_numeric.py

+26
Original file line numberDiff line numberDiff line change
@@ -1209,3 +1209,29 @@ def test_range_float_union_dtype():
12091209

12101210
result = other.union(index)
12111211
tm.assert_index_equal(result, expected)
1212+
1213+
1214+
def test_uint_index_does_not_convert_to_float64():
1215+
# https://github.com/pandas-dev/pandas/issues/28279
1216+
# https://github.com/pandas-dev/pandas/issues/28023
1217+
series = pd.Series(
1218+
[0, 1, 2, 3, 4, 5],
1219+
index=[
1220+
7606741985629028552,
1221+
17876870360202815256,
1222+
17876870360202815256,
1223+
13106359306506049338,
1224+
8991270399732411471,
1225+
8991270399732411472,
1226+
],
1227+
)
1228+
1229+
result = series.loc[[7606741985629028552, 17876870360202815256]]
1230+
1231+
expected = UInt64Index(
1232+
[7606741985629028552, 17876870360202815256, 17876870360202815256],
1233+
dtype="uint64",
1234+
)
1235+
tm.assert_index_equal(result.index, expected)
1236+
1237+
tm.assert_equal(result, series[:3])

0 commit comments

Comments
 (0)