Skip to content

BUG: NumericIndex should not support float16 dtype #49536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BUG: NumericIndex should not support float16 dtype
  • Loading branch information
Terji Petersen authored and Terji Petersen committed Dec 23, 2022
commit 2cc08ab75a95aada94cc30fa4862544fb4f671c0
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 @@ -176,6 +176,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 @@ -202,6 +206,9 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
dtype = pandas_dtype(dtype)
if not isinstance(dtype, np.dtype):
raise TypeError(f"{dtype} not a numpy type")
if dtype == np.float16:
# float16 not supported (no indexing engine)
dtype = np.dtype(np.float32)

if cls._is_backward_compat_public_numeric_index:
# dtype for NumericIndex
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,20 @@ def test_coerce_list(self):
assert type(arr) is Index


class TestFloat16Index:
# float 16 indexes not supported
# GH 49535
def test_array(self):
arr = np.array([1, 2, 3], dtype=np.float16)
result = NumericIndex(arr)

expected = NumericIndex([1, 2, 3], dtype=np.float32)
tm.assert_index_equal(result, expected, check_exact=True)

result = NumericIndex([1, 2, 3], dtype=np.float16)
tm.assert_index_equal(result, expected, check_exact=True)


class TestUIntNumericIndex(NumericInt):

_index_cls = NumericIndex
Expand Down