Skip to content

Commit a173ea8

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
make NumericIndex fail with float16 dtype
1 parent 3544ab2 commit a173ea8

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

pandas/core/indexes/numeric.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
176176
subarr = np.asarray(subarr)
177177
if subarr.dtype == "float16":
178178
# float16 not supported (no indexing engine)
179-
subarr = subarr.astype("float32")
179+
raise TypeError("float16 indexes are not supported")
180180

181181
return subarr
182182

@@ -202,9 +202,9 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
202202
return cls._default_dtype
203203

204204
dtype = pandas_dtype(dtype)
205-
if dtype == np.float16:
205+
if dtype == "float16":
206206
# float16 not supported (no indexing engine)
207-
dtype = np.dtype(np.float32)
207+
raise TypeError("float16 indexes are not supported")
208208
assert isinstance(dtype, np.dtype)
209209

210210
if cls._is_backward_compat_public_numeric_index:

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,8 @@ class TestFloat16Index:
557557
# GH 49535
558558
def test_array(self):
559559
arr = np.array([1, 2, 3], dtype=np.float16)
560-
result = NumericIndex(arr)
561-
562-
expected = NumericIndex([1, 2, 3], dtype=np.float32)
563-
tm.assert_index_equal(result, expected, check_exact=True)
564-
565-
result = NumericIndex([1, 2, 3], dtype=np.float16)
566-
tm.assert_index_equal(result, expected, check_exact=True)
560+
with pytest.raises(TypeError, match="float16 indexes are not supported"):
561+
result = NumericIndex(arr)
567562

568563

569564
class TestUIntNumericIndex(NumericInt):

0 commit comments

Comments
 (0)