Skip to content

Commit 75adc95

Browse files
authored
Merge branch 'main' into clean-iterable
2 parents b47951a + b82986c commit 75adc95

File tree

3 files changed

+110
-16
lines changed

3 files changed

+110
-16
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ from pandas._typing import (
106106
PandasFloatDtypeArg,
107107
PyArrowFloatDtypeArg,
108108
ReindexMethod,
109+
Renamer,
109110
S2_contra,
110111
Scalar,
111112
SequenceNotStr,
@@ -532,28 +533,48 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
532533
) -> Index[C2]: ...
533534
@overload
534535
def append(self, other: Index | Sequence[Index]) -> Index: ...
535-
def putmask(self, mask, value): ...
536+
def putmask(
537+
self,
538+
mask: Sequence[bool] | np_ndarray_bool | BooleanArray | IndexOpsMixin[bool],
539+
value: Scalar,
540+
) -> Index: ...
536541
def equals(self, other: Any) -> bool: ...
537542
@final
538543
def identical(self, other: Any) -> bool: ...
539544
@final
540-
def asof(self, label): ...
541-
def asof_locs(self, where, mask): ...
545+
def asof(self, label: Scalar) -> Scalar: ...
546+
def asof_locs(
547+
self, where: DatetimeIndex, mask: np_ndarray_bool
548+
) -> np_1darray_intp: ...
549+
@overload
542550
def sort_values(
543551
self,
544552
*,
545-
return_indexer: bool = ...,
546-
ascending: bool = ...,
547-
na_position: NaPosition = ...,
553+
return_indexer: Literal[False] = False,
554+
ascending: bool = True,
555+
na_position: NaPosition = "last",
548556
key: Callable[[Index], Index] | None = None,
549-
): ...
557+
) -> Self: ...
558+
@overload
559+
def sort_values(
560+
self,
561+
*,
562+
return_indexer: Literal[True],
563+
ascending: bool = True,
564+
na_position: NaPosition = "last",
565+
key: Callable[[Index], Index] | None = None,
566+
) -> tuple[Self, np_1darray_intp]: ...
550567
@final
551568
def sort(self, *args: Any, **kwargs: Any) -> None: ...
552569
def argsort(self, *args: Any, **kwargs: Any) -> np_1darray_intp: ...
553-
def get_indexer_non_unique(self, target): ...
570+
def get_indexer_non_unique(
571+
self, target: Index
572+
) -> tuple[np_1darray_intp, np_1darray_intp]: ...
554573
@final
555-
def get_indexer_for(self, target, **kwargs: Any): ...
556-
def map(self, mapper, na_action=...) -> Index: ...
574+
def get_indexer_for(self, target: Index) -> np_1darray_intp: ...
575+
def map(
576+
self, mapper: Renamer, na_action: Literal["ignore"] | None = None
577+
) -> Index: ...
557578
def isin(
558579
self, values: Iterable[Any], level: Level | None = None
559580
) -> np_1darray_bool: ...
@@ -562,11 +583,11 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
562583
start: Label | None = None,
563584
end: Label | None = None,
564585
step: int | None = None,
565-
): ...
566-
def get_slice_bound(self, label, side): ...
586+
) -> slice: ...
587+
def get_slice_bound(self, label: Scalar, side: Literal["left", "right"]) -> int: ...
567588
def slice_locs(
568589
self, start: SliceType = None, end: SliceType = None, step: int | None = None
569-
): ...
590+
) -> tuple[int | np.intp, int | np.intp]: ...
570591
def delete(
571592
self, loc: np.integer | int | AnyArrayLikeInt | Sequence[int]
572593
) -> Self: ...

pandas-stubs/core/indexes/interval.pyi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ class IntervalIndex(ExtensionIndex[IntervalT, np.object_], IntervalMixin):
214214
@property
215215
def is_overlapping(self) -> bool: ...
216216
def get_loc(self, key: Label) -> int | slice | np_1darray_bool: ...
217-
def get_indexer_non_unique(
218-
self, target: Index
219-
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...
220217
@property
221218
def left(self) -> Index: ...
222219
@property

tests/indexes/test_indexes.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
assert_type,
2424
)
2525

26+
from pandas._typing import Scalar # noqa: F401
27+
2628
from tests import (
2729
PD_LTE_23,
2830
TYPE_CHECKING_INVALID_USAGE,
@@ -1585,3 +1587,77 @@ def test_index_setitem() -> None:
15851587
idx = pd.Index([1, 2])
15861588
if TYPE_CHECKING_INVALID_USAGE:
15871589
idx[0] = 999 # type: ignore[index] # pyright: ignore[reportIndexIssue]
1590+
1591+
1592+
def test_index_putmask() -> None:
1593+
idx = pd.Index([1, 2])
1594+
check(assert_type(idx.putmask([True, False], 11.4), "pd.Index"), pd.Index)
1595+
check(assert_type(idx.putmask(np.array([True, False]), 11.4), "pd.Index"), pd.Index)
1596+
check(
1597+
assert_type(idx.putmask(pd.Series([True, False]), 11.4), "pd.Index"), pd.Index
1598+
)
1599+
check(assert_type(idx.putmask(pd.Index([True, False]), 11.4), "pd.Index"), pd.Index)
1600+
check(assert_type(idx.putmask(pd.array([True, False]), 11.5), "pd.Index"), pd.Index)
1601+
1602+
1603+
def test_index_asof() -> None:
1604+
check(assert_type(pd.Index([1, 2]).asof(1), "Scalar"), np.integer)
1605+
check(assert_type(pd.Index(["a", "b", "c"]).asof("c"), "Scalar"), str)
1606+
1607+
1608+
def test_index_asof_locs() -> None:
1609+
idx = pd.DatetimeIndex(["2020-01-01", "2020-01-02", "2020-01-03"])
1610+
check(
1611+
assert_type(
1612+
idx.asof_locs(
1613+
pd.DatetimeIndex(["2020-01-01 11:00"]), np.array([True, True, True])
1614+
),
1615+
np_1darray_intp,
1616+
),
1617+
np_1darray_intp,
1618+
)
1619+
1620+
1621+
def test_index_sort_values() -> None:
1622+
idx = pd.DatetimeIndex(["2020-01-01", "2020-01-02", "2020-01-03"])
1623+
check(assert_type(idx.sort_values(), pd.DatetimeIndex), pd.DatetimeIndex)
1624+
sorted_index, indexer = idx.sort_values(return_indexer=True)
1625+
check(assert_type(sorted_index, pd.DatetimeIndex), pd.DatetimeIndex)
1626+
check(assert_type(indexer, np_1darray_intp), np_1darray_intp)
1627+
1628+
1629+
def test_index_get_indexer_non_unique() -> None:
1630+
idx = pd.Index([1, 3])
1631+
indexer, missing = idx.get_indexer_non_unique(pd.Index([3]))
1632+
check(assert_type(indexer, np_1darray_intp), np_1darray_intp)
1633+
check(assert_type(missing, np_1darray_intp), np_1darray_intp)
1634+
1635+
1636+
def test_index_get_indexer_for() -> None:
1637+
idx = pd.Index([1, 3])
1638+
check(
1639+
assert_type(idx.get_indexer_for(pd.Index([3])), np_1darray_intp),
1640+
np_1darray_intp,
1641+
)
1642+
1643+
1644+
def test_index_map() -> None:
1645+
idx = pd.Index([1, 3])
1646+
check(assert_type(idx.map(lambda x: str(x)), pd.Index), pd.Index)
1647+
1648+
1649+
def test_index_slice_indexer() -> None:
1650+
idx = pd.Index([1, 3])
1651+
check(assert_type(idx.slice_indexer(0, 1), slice), slice)
1652+
1653+
1654+
def test_index_get_slice_bound() -> None:
1655+
idx = pd.Index([1, 3])
1656+
check(assert_type(idx.get_slice_bound(1, side="left"), int), int)
1657+
1658+
1659+
def test_index_slice_locs() -> None:
1660+
idx = pd.Index([1, 3])
1661+
start, end = idx.slice_locs(0, 1)
1662+
check(assert_type(start, np.intp | int), np.integer)
1663+
check(assert_type(end, np.intp | int), int)

0 commit comments

Comments
 (0)