Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ from pandas._typing import (
MaskType,
NaPosition,
NDArrayT,
NumpyFloat16DtypeArg,
NumpyFloatNot16DtypeArg,
NumpyNotTimeDtypeArg,
NumpyTimedeltaDtypeArg,
Expand Down Expand Up @@ -189,6 +190,16 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
tupleize_cols: bool = ...,
) -> Index[float]: ...
@overload
def __new__(
cls,
data: AxesData = ...,
*,
dtype: NumpyFloat16DtypeArg,
copy: bool = ...,
name: Hashable = ...,
tupleize_cols: bool = ...,
) -> Never: ...
@overload
def __new__(
cls,
data: AxesData,
Expand Down Expand Up @@ -395,6 +406,12 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
cls: NumpyNotTimeDtypeArg | NumpyTimedeltaDtypeArg | NumpyTimestampDtypeArg,
) -> np_1darray: ...
@overload
def astype(
self,
dtype: NumpyFloat16DtypeArg,
copy: bool = True,
) -> Never: ...
@overload
def astype(
self,
dtype: FloatNotNumpy16DtypeArg | PandasAstypeFloatDtypeArg,
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class IntervalIndex(ExtensionIndex[IntervalT, np.object_], IntervalMixin):
def __contains__(self, key: IntervalT) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __contains__(self, key: object) -> Literal[False]: ...
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ...
def astype(self, dtype: DtypeArg, copy: bool = True) -> IntervalIndex: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
@property
def inferred_type(self) -> str: ...
def memory_usage(self, deep: bool = False) -> int: ...
Expand Down
51 changes: 42 additions & 9 deletions tests/indexes/test_index_float.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from typing import TYPE_CHECKING
from typing import (
TYPE_CHECKING,
)

import numpy as np
import pandas as pd
import pytest
from typing_extensions import assert_type
from typing_extensions import (
Never,
assert_type,
)

from tests import (
ASTYPE_FLOAT_NOT_NUMPY16_ARGS,
TYPE_CHECKING_INVALID_USAGE,
TYPE_FLOAT_NOT_NUMPY16_ARGS,
check,
exception_on_platform,
Expand Down Expand Up @@ -149,10 +155,37 @@ def test_astype_float(
assert_type(s.astype("float64[pyarrow]"), "pd.Index[float]")
assert_type(s.astype("double[pyarrow]"), "pd.Index[float]")

# if TYPE_CHECKING_INVALID_USAGE:
# # numpy float16
# s.astype(np.half)
# s.astype("half")
# s.astype("float16")
# s.astype("e")
# s.astype("f2")

def test_new_astype_float16() -> None:
"""Test that a series cannot be built or cast to a float16 type."""
s = pd.Index([1, 2, 3])

if TYPE_CHECKING_INVALID_USAGE:
assert_type(s.astype(np.half), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(s.astype("half"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(s.astype("float16"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(s.astype("e"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(s.astype("f2"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Index([1.0], dtype=np.half), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Index([1.0], dtype="half"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Index([1.0], dtype="float16"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Index([1.0], dtype="e"), Never)

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Index([1.0], dtype="f2"), Never)