Skip to content

Commit 539fba6

Browse files
authored
BUG: UInt64Index construction casting to float64 (#42201)
1 parent 7a38d63 commit 539fba6

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Numeric
143143

144144
Conversion
145145
^^^^^^^^^^
146-
-
146+
- Bug in :class:`UInt64Index` constructor when passing a list containing both positive integers small enough to cast to int64 and integers too large too hold in int64 (:issue:`42201`)
147147
-
148148

149149
Strings

pandas/core/indexes/numeric.py

+5
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ def _ensure_array(cls, data, dtype, copy: bool):
153153
if not isinstance(data, (ABCSeries, list, tuple)):
154154
data = list(data)
155155

156+
orig = data
156157
data = np.asarray(data, dtype=dtype)
158+
if dtype is None and data.dtype.kind == "f":
159+
if cls is UInt64Index and (data >= 0).all():
160+
# https://github.com/numpy/numpy/issues/19146
161+
data = np.asarray(orig, dtype=np.uint64)
157162

158163
if issubclass(data.dtype.type, str):
159164
cls._string_data_error(data)

pandas/tests/indexes/numeric/test_numeric.py

-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import pytest
33

44
from pandas._libs.tslibs import Timestamp
5-
from pandas.compat import (
6-
is_platform_arm,
7-
is_platform_mac,
8-
)
95

106
import pandas as pd
117
from pandas import (
@@ -535,10 +531,6 @@ def test_constructor(self, dtype):
535531
res = Index([1, 2 ** 63 + 1], dtype=dtype)
536532
tm.assert_index_equal(res, idx)
537533

538-
@pytest.mark.xfail(
539-
not (is_platform_arm() and is_platform_mac()),
540-
reason="https://github.com/numpy/numpy/issues/19146",
541-
)
542534
def test_constructor_does_not_cast_to_float(self):
543535
# https://github.com/numpy/numpy/issues/19146
544536
values = [0, np.iinfo(np.uint64).max]

0 commit comments

Comments
 (0)