Skip to content

Commit 0c6c60b

Browse files
committed
type Index view and drop
1 parent 18626a9 commit 0c6c60b

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

pandas-stubs/_typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,8 @@ np_1darray_dt: TypeAlias = np_1darray[np.datetime64]
950950
np_1darray_td: TypeAlias = np_1darray[np.timedelta64]
951951
np_2darray: TypeAlias = np.ndarray[tuple[int, int], np.dtype[GenericT]]
952952

953+
NDArrayT = TypeVar("NDArrayT", bound=np.ndarray)
954+
953955
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
954956
KeysArgType: TypeAlias = Any
955957
ListLikeT = TypeVar("ListLikeT", bound=ListLike)

pandas-stubs/core/indexes/base.pyi

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ from pandas._typing import (
102102
Level,
103103
MaskType,
104104
NaPosition,
105+
NDArrayT,
105106
NumpyFloatNot16DtypeArg,
106107
PandasAstypeFloatDtypeArg,
107108
PandasFloatDtypeArg,
@@ -374,7 +375,12 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
374375
def dtype(self) -> DtypeObj: ...
375376
@final
376377
def ravel(self, order: _str = "C") -> Self: ...
377-
def view(self, cls=...): ...
378+
@overload
379+
def view(self, cls: None = None) -> Self: ...
380+
@overload
381+
def view(self, cls: type[NDArrayT]) -> NDArrayT: ...
382+
@overload
383+
def view(self, cls: Dtype) -> ArrayLike: ...
378384
@overload
379385
def astype(
380386
self,
@@ -596,7 +602,11 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
596602
def insert(self, loc: int, item: S1) -> Self: ...
597603
@overload
598604
def insert(self, loc: int, item: object) -> Index: ...
599-
def drop(self, labels, errors: IgnoreRaise = "raise") -> Self: ...
605+
def drop(
606+
self,
607+
labels: IndexOpsMixin | np_1darray | Iterable[Hashable],
608+
errors: IgnoreRaise = "raise",
609+
) -> Self: ...
600610
@property
601611
def shape(self) -> tuple[int, ...]: ...
602612
# Extra methods from old stubs

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ ignore = [
237237
# TODO: remove when _libs is fully typed
238238
"ANN001", "ANN201", "ANN204", "ANN206",
239239
]
240-
"*base.pyi" = [
240+
"*core/base.pyi" = [
241+
# TODO: remove when base.pyi's are fully typed
242+
"ANN001", "ANN201", "ANN204", "ANN206",
243+
]
244+
"*excel/_base.pyi" = [
241245
# TODO: remove when base.pyi's are fully typed
242246
"ANN001", "ANN201", "ANN204", "ANN206",
243247
]

tests/indexes/test_indexes.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
assert_type,
2323
)
2424

25+
from pandas._typing import ArrayLike # noqa: F401
2526
from pandas._typing import Dtype # noqa: F401
2627
from pandas._typing import Scalar # noqa: F401
2728

@@ -1667,3 +1668,28 @@ def test_index_slice_locs() -> None:
16671668
start, end = idx.slice_locs(0, 1)
16681669
check(assert_type(start, np.intp | int), np.integer)
16691670
check(assert_type(end, np.intp | int), int)
1671+
1672+
1673+
def test_index_view() -> None:
1674+
ind = pd.Index([1, 2])
1675+
check(assert_type(ind.view("int64"), ArrayLike), np_1darray_int64)
1676+
check(assert_type(ind.view(), "pd.Index[int]"), pd.Index)
1677+
check(assert_type(ind.view(np.ndarray), np.ndarray), np.ndarray)
1678+
1679+
class MyArray(np.ndarray): ...
1680+
1681+
check(assert_type(ind.view(MyArray), MyArray), MyArray)
1682+
1683+
1684+
def test_index_drop() -> None:
1685+
ind = pd.Index([1, 2, 3])
1686+
check(assert_type(ind.drop([1, 2]), "pd.Index[int]"), pd.Index, np.integer)
1687+
check(
1688+
assert_type(ind.drop(pd.Index([1, 2])), "pd.Index[int]"), pd.Index, np.integer
1689+
)
1690+
check(
1691+
assert_type(ind.drop(pd.Series([1, 2])), "pd.Index[int]"), pd.Index, np.integer
1692+
)
1693+
check(
1694+
assert_type(ind.drop(np.array([1, 2])), "pd.Index[int]"), pd.Index, np.integer
1695+
)

0 commit comments

Comments
 (0)