Skip to content

Commit 122a2b9

Browse files
Terji PetersenTerji Petersen
authored andcommitted
BUG: float16 index
1 parent 816da24 commit 122a2b9

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
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"
@@ -175,6 +175,10 @@ def _ensure_array(cls, data, dtype, copy: bool):
175175
raise ValueError("Index data must be 1-dimensional")
176176

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

180184
@classmethod
@@ -199,6 +203,9 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
199203
return cls._default_dtype
200204

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

204211
if cls._is_backward_compat_public_numeric_index:

pandas/tests/indexes/test_numpy_compat.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ def test_numpy_ufuncs_basic(index, func):
8282
assert type(result) is NumericIndex
8383
if is_complex_dtype(index):
8484
assert result.dtype == "complex64"
85-
elif index.dtype in ("bool", "int8", "uint8", "float16"):
86-
assert result.dtype == "float16"
87-
elif index.dtype in ("int16", "uint16", "float32"):
85+
elif index.dtype in {
86+
"bool",
87+
"int8",
88+
"int16",
89+
"uint8",
90+
"uint16",
91+
"float16",
92+
"float32",
93+
}:
8894
assert result.dtype == "float32"
8995
else:
9096
assert result.dtype == "float64"

0 commit comments

Comments
 (0)