Skip to content

Commit

Permalink
BUG: float16 index
Browse files Browse the repository at this point in the history
  • Loading branch information
Terji Petersen authored and Terji Petersen committed Nov 4, 2022
1 parent 816da24 commit 122a2b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class NumericIndex(Index):
Notes
-----
An NumericIndex instance can **only** contain numpy int64/32/16/8, uint64/32/16/8 or
float64/32/16 dtype. In particular, ``NumericIndex`` *can not* hold Pandas numeric
dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
float64/32 dtype. In particular, ``NumericIndex`` *can not* hold numpy float16
dtype or Pandas numeric dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
"""

_typ = "numericindex"
Expand Down Expand Up @@ -175,6 +175,10 @@ def _ensure_array(cls, data, dtype, copy: bool):
raise ValueError("Index data must be 1-dimensional")

subarr = np.asarray(subarr)
if subarr.dtype == "float16":
# float16 not supported (no indexing engine)
subarr = subarr.astype("float32")

return subarr

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

dtype = pandas_dtype(dtype)
if dtype == np.float16:
# float16 not supported (no indexing engine)
dtype = np.dtype(np.float32)
assert isinstance(dtype, np.dtype)

if cls._is_backward_compat_public_numeric_index:
Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/indexes/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ def test_numpy_ufuncs_basic(index, func):
assert type(result) is NumericIndex
if is_complex_dtype(index):
assert result.dtype == "complex64"
elif index.dtype in ("bool", "int8", "uint8", "float16"):
assert result.dtype == "float16"
elif index.dtype in ("int16", "uint16", "float32"):
elif index.dtype in {
"bool",
"int8",
"int16",
"uint8",
"uint16",
"float16",
"float32",
}:
assert result.dtype == "float32"
else:
assert result.dtype == "float64"
Expand Down

0 comments on commit 122a2b9

Please sign in to comment.