diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f992d6aa09ead..eded75b1f180b 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -142,7 +142,7 @@ Numeric Conversion ^^^^^^^^^^ -- +- 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`) - Strings diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 181451603efa4..24f3df684ab10 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -153,7 +153,12 @@ def _ensure_array(cls, data, dtype, copy: bool): if not isinstance(data, (ABCSeries, list, tuple)): data = list(data) + orig = data data = np.asarray(data, dtype=dtype) + if dtype is None and data.dtype.kind == "f": + if cls is UInt64Index and (data >= 0).all(): + # https://github.com/numpy/numpy/issues/19146 + data = np.asarray(orig, dtype=np.uint64) if issubclass(data.dtype.type, str): cls._string_data_error(data) diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index 9747167296be7..8cbca0ba8eb65 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -2,10 +2,6 @@ import pytest from pandas._libs.tslibs import Timestamp -from pandas.compat import ( - is_platform_arm, - is_platform_mac, -) import pandas as pd from pandas import ( @@ -535,10 +531,6 @@ def test_constructor(self, dtype): res = Index([1, 2 ** 63 + 1], dtype=dtype) tm.assert_index_equal(res, idx) - @pytest.mark.xfail( - not (is_platform_arm() and is_platform_mac()), - reason="https://github.com/numpy/numpy/issues/19146", - ) def test_constructor_does_not_cast_to_float(self): # https://github.com/numpy/numpy/issues/19146 values = [0, np.iinfo(np.uint64).max]