Skip to content

Commit 7038f85

Browse files
oguzhanogredenjreback
authored andcommitted
Makes NumericIndex constructor dtype aware (#29529)
1 parent 55ef668 commit 7038f85

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ Numeric
343343
- Bug in :class:`DataFrame` logical operations (`&`, `|`, `^`) not matching :class:`Series` behavior by filling NA values (:issue:`28741`)
344344
- Bug in :meth:`DataFrame.interpolate` where specifying axis by name references variable before it is assigned (:issue:`29142`)
345345
- Improved error message when using `frac` > 1 and `replace` = False (:issue:`27451`)
346+
- Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`)
346347
-
347348

348349
Conversion

pandas/core/indexes/base.py

-24
Original file line numberDiff line numberDiff line change
@@ -4027,30 +4027,6 @@ def _string_data_error(cls, data):
40274027
"to explicitly cast to a numeric type"
40284028
)
40294029

4030-
@classmethod
4031-
def _coerce_to_ndarray(cls, data):
4032-
"""
4033-
Coerces data to ndarray.
4034-
4035-
Converts other iterables to list first and then to array.
4036-
Does not touch ndarrays.
4037-
4038-
Raises
4039-
------
4040-
TypeError
4041-
When the data passed in is a scalar.
4042-
"""
4043-
4044-
if not isinstance(data, (np.ndarray, Index)):
4045-
if data is None or is_scalar(data):
4046-
raise cls._scalar_data_error(data)
4047-
4048-
# other iterable of some kind
4049-
if not isinstance(data, (ABCSeries, list, tuple)):
4050-
data = list(data)
4051-
data = np.asarray(data)
4052-
return data
4053-
40544030
def _coerce_scalar_to_index(self, item):
40554031
"""
40564032
We need to coerce a scalar to a compat for our index type.

pandas/core/indexes/numeric.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
ABCFloat64Index,
2323
ABCInt64Index,
2424
ABCRangeIndex,
25+
ABCSeries,
2526
ABCUInt64Index,
2627
)
2728
from pandas.core.dtypes.missing import isna
@@ -55,8 +56,16 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None):
5556
if fastpath:
5657
return cls._simple_new(data, name=name)
5758

58-
# is_scalar, generators handled in coerce_to_ndarray
59-
data = cls._coerce_to_ndarray(data)
59+
# Coerce to ndarray if not already ndarray or Index
60+
if not isinstance(data, (np.ndarray, Index)):
61+
if is_scalar(data):
62+
raise cls._scalar_data_error(data)
63+
64+
# other iterable of some kind
65+
if not isinstance(data, (ABCSeries, list, tuple)):
66+
data = list(data)
67+
68+
data = np.asarray(data, dtype=dtype)
6069

6170
if issubclass(data.dtype.type, str):
6271
cls._string_data_error(data)

pandas/tests/indexes/test_numeric.py

+5
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,11 @@ def test_constructor(self):
944944
res = Index(np.array([-1, 2 ** 63], dtype=object))
945945
tm.assert_index_equal(res, idx)
946946

947+
# https://github.com/pandas-dev/pandas/issues/29526
948+
idx = UInt64Index([1, 2 ** 63 + 1], dtype=np.uint64)
949+
res = Index([1, 2 ** 63 + 1], dtype=np.uint64)
950+
tm.assert_index_equal(res, idx)
951+
947952
def test_get_indexer(self, index_large):
948953
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2 ** 63)
949954
indexer = index_large.get_indexer(target)

0 commit comments

Comments
 (0)