Skip to content

Commit 5256fd0

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
make NumericIndex fail with float16 dtype
1 parent e9917c4 commit 5256fd0

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

pandas/core/indexes/numeric.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _ensure_array(cls, data, dtype, copy: bool):
179179
subarr = np.asarray(subarr)
180180
if subarr.dtype == "float16":
181181
# float16 not supported (no indexing engine)
182-
subarr = subarr.astype("float32")
182+
raise TypeError("float16 indexes are not supported")
183183

184184
return subarr
185185

@@ -210,6 +210,9 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
210210
if dtype == np.float16:
211211
# float16 not supported (no indexing engine)
212212
dtype = np.dtype(np.float32)
213+
if dtype == "float16":
214+
# float16 not supported (no indexing engine)
215+
raise TypeError("float16 indexes are not supported")
213216

214217
if cls._is_backward_compat_public_numeric_index:
215218
# dtype for NumericIndex

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,8 @@ class TestFloat16Index:
476476
# GH 49535
477477
def test_array(self):
478478
arr = np.array([1, 2, 3], dtype=np.float16)
479-
result = NumericIndex(arr)
480-
481-
expected = NumericIndex([1, 2, 3], dtype=np.float32)
482-
tm.assert_index_equal(result, expected, check_exact=True)
483-
484-
result = NumericIndex([1, 2, 3], dtype=np.float16)
485-
tm.assert_index_equal(result, expected, check_exact=True)
479+
with pytest.raises(TypeError, match="float16 indexes are not supported"):
480+
result = NumericIndex(arr)
486481

487482

488483
class TestUIntNumericIndex(NumericInt):

0 commit comments

Comments
 (0)