-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: NumericIndex([1 2, 3]).dtype should be int64 on 32-bit systems #49815
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
) | ||
from pandas.core.dtypes.generic import ABCSeries | ||
|
||
from pandas.core.construction import sanitize_array | ||
from pandas.core.indexes.base import ( | ||
Index, | ||
maybe_extract_name, | ||
|
@@ -144,15 +145,17 @@ def _ensure_array(cls, data, dtype, copy: bool): | |
data = list(data) | ||
|
||
orig = data | ||
data = np.asarray(data, dtype=dtype) | ||
if isinstance(data, (list, tuple)): | ||
if len(data): | ||
data = sanitize_array(data, index=None) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
data = np.array([], dtype=np.int64) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this simplifies error handling by not special casing string data. |
||
dtype = cls._ensure_dtype(dtype) | ||
|
||
if copy or not is_dtype_equal(data.dtype, dtype): | ||
|
@@ -198,7 +201,8 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None: | |
return cls._default_dtype | ||
|
||
dtype = pandas_dtype(dtype) | ||
assert isinstance(dtype, np.dtype) | ||
if not isinstance(dtype, np.dtype): | ||
raise TypeError(f"{dtype} not a numpy type") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I strengthen this check here, though it will not be important in the end when everything is moved into |
||
|
||
if cls._is_backward_compat_public_numeric_index: | ||
# dtype for NumericIndex | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,10 @@ def check_coerce(self, a, b, is_float_index=True): | |
else: | ||
self.check_is_index(b) | ||
|
||
def test_constructor_from_list_no_dtype(self): | ||
index = self._index_cls([1.5, 2.5, 3.5]) | ||
assert index.dtype == np.float64 | ||
|
||
def test_constructor(self, dtype): | ||
index_cls = self._index_cls | ||
|
||
|
@@ -115,17 +119,10 @@ def test_constructor_invalid(self): | |
with pytest.raises(TypeError, match=msg): | ||
index_cls(0.0) | ||
|
||
# 2021-02-1 we get ValueError in numpy 1.20, but not on all builds | ||
msg = "|".join( | ||
[ | ||
"String dtype not supported, you may need to explicitly cast ", | ||
"could not convert string to float: 'a'", | ||
] | ||
) | ||
with pytest.raises((TypeError, ValueError), match=msg): | ||
msg = f"data is not compatible with {index_cls.__name__}" | ||
with pytest.raises(ValueError, match=msg): | ||
index_cls(["a", "b", 0.0]) | ||
|
||
msg = f"data is not compatible with {index_cls.__name__}" | ||
with pytest.raises(ValueError, match=msg): | ||
index_cls([Timestamp("20130101")]) | ||
|
||
|
@@ -327,18 +324,16 @@ def test_identical(self, simple_index, dtype): | |
assert not index.astype(dtype=object).identical(index.astype(dtype=dtype)) | ||
|
||
def test_cant_or_shouldnt_cast(self): | ||
msg = ( | ||
"String dtype not supported, " | ||
"you may need to explicitly cast to a numeric type" | ||
) | ||
msg = f"data is not compatible with {self._index_cls.__name__}" | ||
|
||
# can't | ||
data = ["foo", "bar", "baz"] | ||
with pytest.raises(TypeError, match=msg): | ||
with pytest.raises(ValueError, match=msg): | ||
self._index_cls(data) | ||
|
||
# shouldn't | ||
data = ["0", "1", "2"] | ||
with pytest.raises(TypeError, match=msg): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably requires a whatsnew note There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or could just keep the string_error_whatever to keep this tightly focused and avoid the API change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error can in pandas <2.0 only be hit by instantiating Doesn't hit this either it we combine string data with numeric dtype: >>> import pandas as pd
>>> pd.Index(["1", "2"], dtype="int64")
ValueError: Trying to coerce float values to integers # different code path There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
OK, but on the off-chance that the Int64Index removal doesn't happen in time, pls be sure to add a note. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
with pytest.raises(ValueError, match=msg): | ||
self._index_cls(data) | ||
|
||
def test_view_index(self, simple_index): | ||
|
@@ -372,6 +367,10 @@ def simple_index(self, dtype): | |
def index(self, request, dtype): | ||
return self._index_cls(request.param, dtype=dtype) | ||
|
||
def test_constructor_from_list_no_dtype(self): | ||
index = self._index_cls([1, 2, 3]) | ||
assert index.dtype == np.int64 | ||
|
||
def test_constructor(self, dtype): | ||
index_cls = self._index_cls | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was only used in
NumericIndex
.