Skip to content

Commit d849b2e

Browse files
GH1501 Disable creating and converting Index to float16 types (#1573)
* GH1501 Disable creating and converting Index to float16 types * GH1501 Disable creating and converting Index to float16 types * GH1501 PR Feedback * GH1501 PR Feedback * GH1501 PR Feedback
1 parent 43ec202 commit d849b2e

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ from pandas._typing import (
104104
MaskType,
105105
NaPosition,
106106
NDArrayT,
107+
NumpyFloat16DtypeArg,
107108
NumpyFloatNot16DtypeArg,
108109
NumpyNotTimeDtypeArg,
109110
NumpyTimedeltaDtypeArg,
@@ -189,6 +190,16 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
189190
tupleize_cols: bool = ...,
190191
) -> Index[float]: ...
191192
@overload
193+
def __new__(
194+
cls,
195+
data: AxesData = ...,
196+
*,
197+
dtype: NumpyFloat16DtypeArg,
198+
copy: bool = ...,
199+
name: Hashable = ...,
200+
tupleize_cols: bool = ...,
201+
) -> Never: ...
202+
@overload
192203
def __new__(
193204
cls,
194205
data: AxesData,
@@ -395,6 +406,12 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
395406
cls: NumpyNotTimeDtypeArg | NumpyTimedeltaDtypeArg | NumpyTimestampDtypeArg,
396407
) -> np_1darray: ...
397408
@overload
409+
def astype(
410+
self,
411+
dtype: NumpyFloat16DtypeArg,
412+
copy: bool = True,
413+
) -> Never: ...
414+
@overload
398415
def astype(
399416
self,
400417
dtype: FloatNotNumpy16DtypeArg | PandasAstypeFloatDtypeArg,

pandas-stubs/core/indexes/interval.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class IntervalIndex(ExtensionIndex[IntervalT, np.object_], IntervalMixin):
243243
def __contains__(self, key: IntervalT) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
244244
@overload
245245
def __contains__(self, key: object) -> Literal[False]: ...
246-
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ...
246+
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
247247
@property
248248
def inferred_type(self) -> str: ...
249249
def memory_usage(self, deep: bool = False) -> int: ...

tests/indexes/test_index_float.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
from typing import TYPE_CHECKING
1+
from typing import (
2+
TYPE_CHECKING,
3+
)
24

35
import numpy as np
46
import pandas as pd
57
import pytest
6-
from typing_extensions import assert_type
8+
from typing_extensions import (
9+
Never,
10+
assert_type,
11+
)
712

813
from tests import (
914
ASTYPE_FLOAT_NOT_NUMPY16_ARGS,
15+
TYPE_CHECKING_INVALID_USAGE,
1016
TYPE_FLOAT_NOT_NUMPY16_ARGS,
1117
check,
1218
exception_on_platform,
@@ -89,14 +95,22 @@ def test_constructor_dtype(
8995
assert_type(pd.Index([1.0], dtype="float64[pyarrow]"), "pd.Index[float]")
9096
assert_type(pd.Index([1.0], dtype="double[pyarrow]"), "pd.Index[float]")
9197

92-
# TODO: pandas-dev/pandas-stubs#1501
93-
# if TYPE_CHECKING_INVALID_USAGE:
94-
# # numpy float16
95-
# pd.Index([1.0], dtype=np.half)
96-
# pd.Index([1.0], dtype="half")
97-
# pd.Index([1.0], dtype="float16")
98-
# pd.Index([1.0], dtype="e")
99-
# pd.Index([1.0], dtype="f2")
98+
if TYPE_CHECKING_INVALID_USAGE:
99+
# numpy float16
100+
def _0() -> None: # pyright: ignore[reportUnusedFunction]
101+
assert_type(pd.Index([1.0], dtype=np.half), Never)
102+
103+
def _1() -> None: # pyright: ignore[reportUnusedFunction]
104+
assert_type(pd.Index([1.0], dtype="half"), Never)
105+
106+
def _2() -> None: # pyright: ignore[reportUnusedFunction]
107+
assert_type(pd.Index([1.0], dtype="float16"), Never)
108+
109+
def _3() -> None: # pyright: ignore[reportUnusedFunction]
110+
assert_type(pd.Index([1.0], dtype="e"), Never)
111+
112+
def _4() -> None: # pyright: ignore[reportUnusedFunction]
113+
assert_type(pd.Index([1.0], dtype="f2"), Never)
100114

101115

102116
@pytest.mark.parametrize(
@@ -149,10 +163,24 @@ def test_astype_float(
149163
assert_type(s.astype("float64[pyarrow]"), "pd.Index[float]")
150164
assert_type(s.astype("double[pyarrow]"), "pd.Index[float]")
151165

152-
# if TYPE_CHECKING_INVALID_USAGE:
153-
# # numpy float16
154-
# s.astype(np.half)
155-
# s.astype("half")
156-
# s.astype("float16")
157-
# s.astype("e")
158-
# s.astype("f2")
166+
167+
def test_new_astype_float16() -> None:
168+
"""Test that a series cannot be built or cast to a float16 type."""
169+
s = pd.Index([1, 2, 3])
170+
171+
if TYPE_CHECKING_INVALID_USAGE:
172+
173+
def _0() -> None: # pyright: ignore[reportUnusedFunction]
174+
assert_type(s.astype(np.half), Never)
175+
176+
def _1() -> None: # pyright: ignore[reportUnusedFunction]
177+
assert_type(s.astype("half"), Never)
178+
179+
def _2() -> None: # pyright: ignore[reportUnusedFunction]
180+
assert_type(s.astype("float16"), Never)
181+
182+
def _3() -> None: # pyright: ignore[reportUnusedFunction]
183+
assert_type(s.astype("e"), Never)
184+
185+
def _4() -> None: # pyright: ignore[reportUnusedFunction]
186+
assert_type(s.astype("f2"), Never)

0 commit comments

Comments
 (0)