Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes NumericIndex constructor dtype aware #29529

Merged
merged 18 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ Numeric
- Bug in :class:`DataFrame` logical operations (`&`, `|`, `^`) not matching :class:`Series` behavior by filling NA values (:issue:`28741`)
- Bug in :meth:`DataFrame.interpolate` where specifying axis by name references variable before it is assigned (:issue:`29142`)
- Improved error message when using `frac` > 1 and `replace` = False (:issue:`27451`)
- Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`)
-

Conversion
Expand Down
24 changes: 0 additions & 24 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4026,30 +4026,6 @@ def _string_data_error(cls, data):
"to explicitly cast to a numeric type"
)

@classmethod
def _coerce_to_ndarray(cls, data):
"""
Coerces data to ndarray.

Converts other iterables to list first and then to array.
Does not touch ndarrays.

Raises
------
TypeError
When the data passed in is a scalar.
"""

if not isinstance(data, (np.ndarray, Index)):
if data is None or is_scalar(data):
raise cls._scalar_data_error(data)

# other iterable of some kind
if not isinstance(data, (ABCSeries, list, tuple)):
data = list(data)
data = np.asarray(data)
return data

def _coerce_scalar_to_index(self, item):
"""
We need to coerce a scalar to a compat for our index type.
Expand Down
13 changes: 11 additions & 2 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ABCFloat64Index,
ABCInt64Index,
ABCRangeIndex,
ABCSeries,
ABCUInt64Index,
)
from pandas.core.dtypes.missing import isna
Expand Down Expand Up @@ -56,8 +57,16 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=None):
if fastpath:
return cls._simple_new(data, name=name)

# is_scalar, generators handled in coerce_to_ndarray
data = cls._coerce_to_ndarray(data)
# Coerce to ndarray if not already ndarray or Index
if not isinstance(data, (np.ndarray, Index)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, this is what we had quite some time ago. thanks

if is_scalar(data):
raise cls._scalar_data_error(data)

# other iterable of some kind
if not isinstance(data, (ABCSeries, list, tuple)):
data = list(data)

data = np.asarray(data, dtype=dtype)

if issubclass(data.dtype.type, str):
cls._string_data_error(data)
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,11 @@ def test_constructor(self):
res = Index(np.array([-1, 2 ** 63], dtype=object))
tm.assert_index_equal(res, idx)

# https://github.com/pandas-dev/pandas/issues/29526
idx = UInt64Index([1, 2 ** 63 + 1], dtype=np.uint64)
res = Index([1, 2 ** 63 + 1], dtype=np.uint64)
tm.assert_index_equal(res, idx)

def test_get_indexer(self, index_large):
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2 ** 63)
indexer = index_large.get_indexer(target)
Expand Down