Skip to content
11 changes: 4 additions & 7 deletions pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._typing import (
S1,
S2,
ArrayLike,
AxisIndex,
DropKeep,
DTypeLike,
GenericT,
GenericT_co,
Just,
ListLike,
NDFrameT,
Scalar,
SequenceNotStr,
SupportsDType,
np_1darray,
np_ndarray_anyint,
Expand All @@ -47,8 +46,6 @@ from pandas._typing import (
)
from pandas.util._decorators import cache_readonly

_ListLike: TypeAlias = ArrayLike | dict[str, np.ndarray] | SequenceNotStr[S1]

class NoNewAttributesMixin:
def __setattr__(self, key: str, value: Any) -> None: ...

Expand Down Expand Up @@ -157,16 +154,16 @@ class IndexOpsMixin(OpsMixin, Generic[S1, GenericT_co]):
@overload
def searchsorted(
self,
value: _ListLike,
value: ListLike,
side: Literal["left", "right"] = ...,
sorter: _ListLike | None = ...,
sorter: ListLike | None = None,
) -> np_1darray[np.intp]: ...
@overload
def searchsorted(
self,
value: Scalar,
side: Literal["left", "right"] = ...,
sorter: _ListLike | None = ...,
sorter: ListLike | None = None,
) -> np.intp: ...
def drop_duplicates(self, *, keep: DropKeep = ...) -> Self: ...

Expand Down
21 changes: 15 additions & 6 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ from pandas.core.base import (
Supports_ProtoMul,
Supports_ProtoRAdd,
Supports_ProtoRMul,
_ListLike,
)
from pandas.core.indexes.category import CategoricalIndex
from pandas.core.strings.accessor import StringMethods
Expand Down Expand Up @@ -506,7 +505,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
@overload
def __add__(self: Index[Never], other: _str) -> Never: ...
@overload
def __add__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ...
def __add__(
self: Index[Never], other: complex | ArrayLike | SequenceNotStr[S1] | Index
) -> Index: ...
@overload
def __add__(self, other: Index[Never]) -> Index: ...
@overload
Expand Down Expand Up @@ -556,7 +557,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
@overload
def __radd__(self: Index[Never], other: _str) -> Never: ...
@overload
def __radd__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ...
def __radd__(
self: Index[Never], other: complex | ArrayLike | SequenceNotStr[S1] | Index
) -> Index: ...
@overload
def __radd__(
self: Supports_ProtoRAdd[_T_contra, S2],
Expand Down Expand Up @@ -605,7 +608,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
@overload
def __sub__(self: Index[Never], other: DatetimeIndex) -> Never: ...
@overload
def __sub__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ...
def __sub__(
self: Index[Never], other: complex | ArrayLike | SequenceNotStr[S1] | Index
) -> Index: ...
@overload
def __sub__(self, other: Index[Never]) -> Index: ...
@overload
Expand Down Expand Up @@ -741,7 +746,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
),
) -> Index[complex]: ...
@overload
def __mul__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ...
def __mul__(
self: Index[Never], other: complex | ArrayLike | SequenceNotStr[S1] | Index
) -> Index: ...
@overload
def __mul__(self, other: Index[Never]) -> Index: ...
@overload
Expand Down Expand Up @@ -817,7 +824,9 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
self: Index[T_COMPLEX], other: np_ndarray_complex | Index[complex]
) -> Index[complex]: ...
@overload
def __rmul__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ...
def __rmul__(
self: Index[Never], other: complex | ArrayLike | SequenceNotStr[S1] | Index
) -> Index: ...
@overload
def __rmul__(self, other: Index[Never]) -> Index: ...
@overload
Expand Down
70 changes: 33 additions & 37 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ from pandas.core.base import (
Supports_ProtoMul,
Supports_ProtoRAdd,
Supports_ProtoRMul,
_ListLike,
)
from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame
Expand Down Expand Up @@ -296,7 +295,8 @@ class _LocIndexerSeries(_LocIndexer, Generic[S1]):
value: S1 | ArrayLike | Series[S1] | None,
) -> None: ...

_ListLikeS1: TypeAlias = (
_DataLike: TypeAlias = ArrayLike | dict[str, np.ndarray] | SequenceNotStr[S1]
_DataLikeS1: TypeAlias = (
ArrayLike | dict[_str, np.ndarray] | Sequence[S1] | IndexOpsMixin[S1]
)

Expand Down Expand Up @@ -360,7 +360,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def __new__(
cls,
data: _ListLike,
data: _DataLike,
index: AxesData | None = ...,
*,
dtype: TimestampDtypeArg,
Expand All @@ -370,7 +370,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def __new__(
cls,
data: _ListLike,
data: _DataLike,
index: AxesData | None = ...,
*,
dtype: CategoryDtypeArg,
Expand Down Expand Up @@ -427,7 +427,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def __new__( # type: ignore[overload-overlap]
cls,
data: Scalar | _ListLike | dict[HashableT1, Any] | None,
data: Scalar | _DataLike | dict[HashableT1, Any] | None,
index: AxesData | None = ...,
*,
dtype: type[S1],
Expand Down Expand Up @@ -474,7 +474,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
def __new__(
cls,
data: (
S1 | _ListLikeS1[S1] | dict[HashableT1, S1] | KeysView[S1] | ValuesView[S1]
S1 | _DataLikeS1[S1] | dict[HashableT1, S1] | KeysView[S1] | ValuesView[S1]
),
index: AxesData | None = ...,
dtype: Dtype = ...,
Expand All @@ -486,7 +486,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
cls,
data: (
Scalar
| _ListLike
| _DataLike
| Mapping[HashableT1, Any]
| BaseGroupBy
| NaTType
Expand Down Expand Up @@ -853,7 +853,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def quantile(
self,
q: _ListLike,
q: ListLike,
interpolation: QuantileInterpolation = ...,
) -> Series[S1]: ...
def corr(
Expand Down Expand Up @@ -905,16 +905,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def searchsorted(
self,
value: _ListLike,
value: ListLike,
side: Literal["left", "right"] = ...,
sorter: _ListLike | None = ...,
sorter: ListLike | None = None,
) -> np_1darray[np.intp]: ...
@overload
def searchsorted(
self,
value: Scalar,
side: Literal["left", "right"] = ...,
sorter: _ListLike | None = ...,
sorter: ListLike | None = None,
) -> np.intp: ...
@overload
def compare(
Expand Down Expand Up @@ -1311,7 +1311,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
) -> Series[S1]: ...
def filter(
self,
items: _ListLike | None = None,
items: ListLike | None = None,
like: _str | None = None,
regex: _str | None = None,
axis: AxisIndex | None = None,
Expand All @@ -1326,7 +1326,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
n: int | None = None,
frac: float | None = None,
replace: _bool = False,
weights: _str | _ListLike | np.ndarray | None = None,
weights: _str | ListLike | np.ndarray | None = None,
random_state: RandomState | None = None,
axis: AxisIndex | None = None,
ignore_index: _bool = False,
Expand Down Expand Up @@ -1687,9 +1687,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
# just failed to generate these so I couldn't match
# them up.
@overload
def __add__(
self: Series[Never], other: complex | _ListLike | Index | Series
) -> Series: ...
def __add__(self: Series[Never], other: complex | ListLike) -> Series: ...
@overload
def __add__(self, other: Index[Never] | Series[Never]) -> Series: ...
@overload
Expand Down Expand Up @@ -1775,7 +1773,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def add(
self: Series[Never],
other: complex | _ListLike | Index | Series,
other: complex | ListLike,
level: Level | None = None,
fill_value: float | None = None,
axis: int = 0,
Expand Down Expand Up @@ -1906,9 +1904,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
axis: int = 0,
) -> Series[_str]: ...
@overload
def __radd__(
self: Series[Never], other: complex | _ListLike | Index | Series
) -> Series: ...
def __radd__(self: Series[Never], other: complex | ListLike) -> Series: ...
@overload
def __radd__(self, other: Index[Never] | Series[Never]) -> Series: ...
@overload
Expand Down Expand Up @@ -2008,7 +2004,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def radd(
self: Series[Never],
other: complex | _ListLike | Index | Series,
other: complex | ListLike,
level: Level | None = None,
fill_value: float | None = None,
axis: int = 0,
Expand Down Expand Up @@ -2461,16 +2457,16 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
axis: AxisIndex = ...,
) -> Series[int]: ...
def __ge__( # type: ignore[override]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
self, other: S1 | ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
def __gt__( # type: ignore[override]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
self, other: S1 | ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
def __le__( # type: ignore[override]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
self, other: S1 | ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
def __lt__( # type: ignore[override]
self, other: S1 | _ListLike | Series[S1] | datetime | timedelta | date
self, other: S1 | ListLike | Series[S1] | datetime | timedelta | date
) -> Series[_bool]: ...
@overload
def __mul__( # type: ignore[overload-overlap]
Expand Down Expand Up @@ -2567,7 +2563,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def mul(
self: Series[Never],
other: complex | _ListLike | Index | Series,
other: complex | ListLike,
level: Level | None = None,
fill_value: float | None = None,
axis: int = 0,
Expand Down Expand Up @@ -2777,7 +2773,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def rmul(
self: Series[Never],
other: complex | _ListLike | Index | Series,
other: complex | ListLike,
level: Level | None = None,
fill_value: float | None = None,
axis: int = 0,
Expand Down Expand Up @@ -2892,9 +2888,9 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
fill_value: float | None = None,
axis: int = 0,
) -> Series[complex]: ...
def __mod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __mod__(self, other: num | ListLike | Series[S1]) -> Series[S1]: ...
def __ne__(self, other: object) -> Series[_bool]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __pow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __pow__(self, other: num | ListLike | Series[S1]) -> Series[S1]: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
# pyrefly: ignore # bad-override
Expand All @@ -2911,9 +2907,9 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
) -> Series[bool]: ...
@overload
def __rand__(self, other: int | np_ndarray_anyint | Series[int]) -> Series[int]: ...
def __rdivmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __rmod__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __rpow__(self, other: num | _ListLike | Series[S1]) -> Series[S1]: ...
def __rdivmod__(self, other: num | ListLike | Series[S1]) -> Series[S1]: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __rmod__(self, other: num | ListLike | Series[S1]) -> Series[S1]: ...
def __rpow__(self, other: num | ListLike | Series[S1]) -> Series[S1]: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
# pyrefly: ignore # bad-override
Expand Down Expand Up @@ -3683,7 +3679,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def truediv(
self: Series[Never],
other: complex | _ListLike | Index | Series,
other: complex | ListLike,
level: Level | None = None,
fill_value: float | None = None,
axis: AxisIndex = 0,
Expand Down Expand Up @@ -3946,7 +3942,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
@overload
def rtruediv(
self: Series[Never],
other: complex | _ListLike | Index | Series,
other: complex | ListLike,
level: Level | None = None,
fill_value: float | None = None,
axis: AxisIndex = 0,
Expand Down Expand Up @@ -4165,7 +4161,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
) -> Series[S1]: ...
def divmod(
self,
other: num | _ListLike | Series[S1],
other: num | ListLike | Series[S1],
level: Level | None = ...,
fill_value: float | None = None,
axis: AxisIndex = ...,
Expand Down Expand Up @@ -4323,7 +4319,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
) -> S1: ...
def mod(
self,
other: num | _ListLike | Series[S1],
other: num | ListLike | Series[S1],
level: Level | None = ...,
fill_value: float | None = None,
axis: AxisIndex | None = 0,
Expand All @@ -4339,7 +4335,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
def nunique(self, dropna: _bool = True) -> int: ...
def pow(
self,
other: num | _ListLike | Series[S1],
other: num | ListLike | Series[S1],
level: Level | None = ...,
fill_value: float | None = None,
axis: AxisIndex | None = 0,
Expand Down
8 changes: 8 additions & 0 deletions tests/indexes/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,8 +1500,16 @@ def test_index_searchsorted() -> None:
idx = pd.Index([1, 2, 3])
check(assert_type(idx.searchsorted(1), np.intp), np.intp)
check(assert_type(idx.searchsorted([1]), "np_1darray[np.intp]"), np_1darray)
check(assert_type(idx.searchsorted(range(1, 2)), "np_1darray[np.intp]"), np_1darray)
check(
assert_type(idx.searchsorted(pd.Series([1])), "np_1darray[np.intp]"), np_1darray
)
check(
assert_type(idx.searchsorted(np.array([1])), "np_1darray[np.intp]"), np_1darray
)
check(assert_type(idx.searchsorted(1, side="left"), np.intp), np.intp)
check(assert_type(idx.searchsorted(1, sorter=[1, 0, 2]), np.intp), np.intp)
check(assert_type(idx.searchsorted(1, sorter=range(3)), np.intp), np.intp)


def test_period_index_constructor() -> None:
Expand Down