Skip to content

Commit e9917c4

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
BUG: NumericIndex should not support float16 dtype
1 parent 5fad2e4 commit e9917c4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pandas/core/indexes/numeric.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class NumericIndex(Index):
7575
Notes
7676
-----
7777
An NumericIndex instance can **only** contain numpy int64/32/16/8, uint64/32/16/8 or
78-
float64/32/16 dtype. In particular, ``NumericIndex`` *can not* hold Pandas numeric
79-
dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
78+
float64/32 dtype. In particular, ``NumericIndex`` *can not* hold numpy float16
79+
dtype or Pandas numeric dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
8080
"""
8181

8282
_typ = "numericindex"
@@ -177,6 +177,10 @@ def _ensure_array(cls, data, dtype, copy: bool):
177177
raise ValueError("Index data must be 1-dimensional")
178178

179179
subarr = np.asarray(subarr)
180+
if subarr.dtype == "float16":
181+
# float16 not supported (no indexing engine)
182+
subarr = subarr.astype("float32")
183+
180184
return subarr
181185

182186
@classmethod
@@ -203,6 +207,9 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
203207
dtype = pandas_dtype(dtype)
204208
if not isinstance(dtype, np.dtype):
205209
raise TypeError(f"{dtype} not a numpy type")
210+
if dtype == np.float16:
211+
# float16 not supported (no indexing engine)
212+
dtype = np.dtype(np.float32)
206213

207214
if cls._is_backward_compat_public_numeric_index:
208215
# dtype for NumericIndex

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,20 @@ def test_coerce_list(self):
471471
assert type(arr) is Index
472472

473473

474+
class TestFloat16Index:
475+
# float 16 indexes not supported
476+
# GH 49535
477+
def test_array(self):
478+
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)
486+
487+
474488
class TestUIntNumericIndex(NumericInt):
475489

476490
_index_cls = NumericIndex

0 commit comments

Comments
 (0)