Skip to content

Commit 5d8d323

Browse files
authored
API: NumericIndex([1 2, 3]).dtype should be int64 on 32-bit systems (#49815)
* API: NumericIndex([1 2, 3]).dtype should be int64 on 32-bit systems * cleanups * cleanups II
1 parent f04e8b0 commit 5d8d323

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

pandas/core/indexes/base.py

-8
Original file line numberDiff line numberDiff line change
@@ -4962,14 +4962,6 @@ def _raise_scalar_data_error(cls, data):
49624962
f"kind, {repr(data)} was passed"
49634963
)
49644964

4965-
@final
4966-
@classmethod
4967-
def _string_data_error(cls, data):
4968-
raise TypeError(
4969-
"String dtype not supported, you may need "
4970-
"to explicitly cast to a numeric type"
4971-
)
4972-
49734965
def _validate_fill_value(self, value):
49744966
"""
49754967
Check if the value can be inserted into our array without casting,

pandas/core/indexes/numeric.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
from pandas.core.dtypes.generic import ABCSeries
3434

35+
from pandas.core.construction import sanitize_array
3536
from pandas.core.indexes.base import (
3637
Index,
3738
maybe_extract_name,
@@ -144,15 +145,17 @@ def _ensure_array(cls, data, dtype, copy: bool):
144145
data = list(data)
145146

146147
orig = data
147-
data = np.asarray(data, dtype=dtype)
148+
if isinstance(data, (list, tuple)):
149+
if len(data):
150+
data = sanitize_array(data, index=None)
151+
else:
152+
data = np.array([], dtype=np.int64)
153+
148154
if dtype is None and data.dtype.kind == "f":
149155
if cls is UInt64Index and (data >= 0).all():
150156
# https://github.com/numpy/numpy/issues/19146
151157
data = np.asarray(orig, dtype=np.uint64)
152158

153-
if issubclass(data.dtype.type, str):
154-
cls._string_data_error(data)
155-
156159
dtype = cls._ensure_dtype(dtype)
157160

158161
if copy or not is_dtype_equal(data.dtype, dtype):
@@ -198,7 +201,8 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
198201
return cls._default_dtype
199202

200203
dtype = pandas_dtype(dtype)
201-
assert isinstance(dtype, np.dtype)
204+
if not isinstance(dtype, np.dtype):
205+
raise TypeError(f"{dtype} not a numpy type")
202206

203207
if cls._is_backward_compat_public_numeric_index:
204208
# dtype for NumericIndex

pandas/tests/indexes/numeric/test_numeric.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def check_coerce(self, a, b, is_float_index=True):
6464
else:
6565
self.check_is_index(b)
6666

67+
def test_constructor_from_list_no_dtype(self):
68+
index = self._index_cls([1.5, 2.5, 3.5])
69+
assert index.dtype == np.float64
70+
6771
def test_constructor(self, dtype):
6872
index_cls = self._index_cls
6973

@@ -115,17 +119,10 @@ def test_constructor_invalid(self):
115119
with pytest.raises(TypeError, match=msg):
116120
index_cls(0.0)
117121

118-
# 2021-02-1 we get ValueError in numpy 1.20, but not on all builds
119-
msg = "|".join(
120-
[
121-
"String dtype not supported, you may need to explicitly cast ",
122-
"could not convert string to float: 'a'",
123-
]
124-
)
125-
with pytest.raises((TypeError, ValueError), match=msg):
122+
msg = f"data is not compatible with {index_cls.__name__}"
123+
with pytest.raises(ValueError, match=msg):
126124
index_cls(["a", "b", 0.0])
127125

128-
msg = f"data is not compatible with {index_cls.__name__}"
129126
with pytest.raises(ValueError, match=msg):
130127
index_cls([Timestamp("20130101")])
131128

@@ -327,18 +324,16 @@ def test_identical(self, simple_index, dtype):
327324
assert not index.astype(dtype=object).identical(index.astype(dtype=dtype))
328325

329326
def test_cant_or_shouldnt_cast(self):
330-
msg = (
331-
"String dtype not supported, "
332-
"you may need to explicitly cast to a numeric type"
333-
)
327+
msg = f"data is not compatible with {self._index_cls.__name__}"
328+
334329
# can't
335330
data = ["foo", "bar", "baz"]
336-
with pytest.raises(TypeError, match=msg):
331+
with pytest.raises(ValueError, match=msg):
337332
self._index_cls(data)
338333

339334
# shouldn't
340335
data = ["0", "1", "2"]
341-
with pytest.raises(TypeError, match=msg):
336+
with pytest.raises(ValueError, match=msg):
342337
self._index_cls(data)
343338

344339
def test_view_index(self, simple_index):
@@ -372,6 +367,10 @@ def simple_index(self, dtype):
372367
def index(self, request, dtype):
373368
return self._index_cls(request.param, dtype=dtype)
374369

370+
def test_constructor_from_list_no_dtype(self):
371+
index = self._index_cls([1, 2, 3])
372+
assert index.dtype == np.int64
373+
375374
def test_constructor(self, dtype):
376375
index_cls = self._index_cls
377376

0 commit comments

Comments
 (0)