Skip to content

Commit 7ca7343

Browse files
committed
Merge branch 'main' of github.com:pandas-dev/pandas-stubs into feature/cmp0xff/1503
2 parents a8906d0 + b82986c commit 7ca7343

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ from pandas._typing import (
107107
PandasFloatDtypeArg,
108108
PyArrowFloatDtypeArg,
109109
ReindexMethod,
110+
Renamer,
110111
S2_contra,
111112
Scalar,
112113
SequenceNotStr,
@@ -533,39 +534,59 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
533534
) -> Index[C2]: ...
534535
@overload
535536
def append(self, other: Index | Sequence[Index]) -> Index: ...
536-
def putmask(self, mask, value): ...
537+
def putmask(
538+
self,
539+
mask: Sequence[bool] | np_ndarray_bool | BooleanArray | IndexOpsMixin[bool],
540+
value: Scalar,
541+
) -> Index: ...
537542
def equals(self, other: Any) -> bool: ...
538543
@final
539544
def identical(self, other: Any) -> bool: ...
540545
@final
541-
def asof(self, label): ...
542-
def asof_locs(self, where, mask): ...
546+
def asof(self, label: Scalar) -> Scalar: ...
547+
def asof_locs(
548+
self, where: DatetimeIndex, mask: np_ndarray_bool
549+
) -> np_1darray_intp: ...
550+
@overload
543551
def sort_values(
544552
self,
545553
*,
546-
return_indexer: bool = ...,
547-
ascending: bool = ...,
548-
na_position: NaPosition = ...,
554+
return_indexer: Literal[False] = False,
555+
ascending: bool = True,
556+
na_position: NaPosition = "last",
549557
key: Callable[[Index], Index] | None = None,
550-
): ...
558+
) -> Self: ...
559+
@overload
560+
def sort_values(
561+
self,
562+
*,
563+
return_indexer: Literal[True],
564+
ascending: bool = True,
565+
na_position: NaPosition = "last",
566+
key: Callable[[Index], Index] | None = None,
567+
) -> tuple[Self, np_1darray_intp]: ...
551568
@final
552569
def sort(self, *args: Any, **kwargs: Any) -> None: ...
553570
def argsort(self, *args: Any, **kwargs: Any) -> np_1darray_intp: ...
554-
def get_indexer_non_unique(self, target): ...
571+
def get_indexer_non_unique(
572+
self, target: Index
573+
) -> tuple[np_1darray_intp, np_1darray_intp]: ...
555574
@final
556-
def get_indexer_for(self, target, **kwargs: Any): ...
557-
def map(self, mapper, na_action=...) -> Index: ...
575+
def get_indexer_for(self, target: Index) -> np_1darray_intp: ...
576+
def map(
577+
self, mapper: Renamer, na_action: Literal["ignore"] | None = None
578+
) -> Index: ...
558579
def isin(self, values, level=...) -> np_1darray_bool: ...
559580
def slice_indexer(
560581
self,
561582
start: Label | None = None,
562583
end: Label | None = None,
563584
step: int | None = None,
564-
): ...
565-
def get_slice_bound(self, label, side): ...
585+
) -> slice: ...
586+
def get_slice_bound(self, label: Scalar, side: Literal["left", "right"]) -> int: ...
566587
def slice_locs(
567588
self, start: SliceType = None, end: SliceType = None, step: int | None = None
568-
): ...
589+
) -> tuple[int | np.intp, int | np.intp]: ...
569590
def delete(
570591
self, loc: np.integer | int | AnyArrayLikeInt | Sequence[int]
571592
) -> 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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
)
2424

2525
from pandas._typing import Dtype # noqa: F401
26+
from pandas._typing import Scalar # noqa: F401
2627

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

0 commit comments

Comments
 (0)