From d1824640943fdf4d5f9dbb3f6c1857785f0dece4 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Jun 2021 13:21:16 -0700 Subject: [PATCH 1/2] BUG: UInt64Index construction casting to float64 --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/core/indexes/numeric.py | 5 +++++ pandas/tests/indexes/numeric/test_numeric.py | 8 -------- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f992d6aa09ead..c5ffee37ad416 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:`??`) - 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] From 4a5ccfc0a2c87b9a27d5ade046d8edf2721a42e0 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Jun 2021 13:22:19 -0700 Subject: [PATCH 2/2] GH ref --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index c5ffee37ad416..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:`??`) +- 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