Skip to content

Commit f087051

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
BUG: NumericIndex should not support float16 dtype
1 parent f09d514 commit f087051

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
@@ -74,8 +74,8 @@ class NumericIndex(Index):
7474
Notes
7575
-----
7676
An NumericIndex instance can **only** contain numpy int64/32/16/8, uint64/32/16/8 or
77-
float64/32/16 dtype. In particular, ``NumericIndex`` *can not* hold Pandas numeric
78-
dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
77+
float64/32 dtype. In particular, ``NumericIndex`` *can not* hold numpy float16
78+
dtype or Pandas numeric dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
7979
"""
8080

8181
_typ = "numericindex"
@@ -174,6 +174,10 @@ def _ensure_array(cls, data, dtype, copy: bool):
174174
raise ValueError("Index data must be 1-dimensional")
175175

176176
subarr = np.asarray(subarr)
177+
if subarr.dtype == "float16":
178+
# float16 not supported (no indexing engine)
179+
subarr = subarr.astype("float32")
180+
177181
return subarr
178182

179183
@classmethod
@@ -198,6 +202,9 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
198202
return cls._default_dtype
199203

200204
dtype = pandas_dtype(dtype)
205+
if dtype == np.float16:
206+
# float16 not supported (no indexing engine)
207+
dtype = np.dtype(np.float32)
201208
assert isinstance(dtype, np.dtype)
202209

203210
if cls._is_backward_compat_public_numeric_index:

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,20 @@ def test_constructor_32bit(self, dtype):
552552
assert index.dtype == np.int64
553553

554554

555+
class TestFloat16Index:
556+
# float 16 indexes not supported
557+
# GH 49535
558+
def test_array(self):
559+
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)
567+
568+
555569
class TestUIntNumericIndex(NumericInt):
556570

557571
_index_cls = NumericIndex

0 commit comments

Comments
 (0)