Skip to content
Merged
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
8 changes: 2 additions & 6 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,11 @@ SeriesDType: TypeAlias = (
| datetime.timedelta # includes pd.Timedelta
)
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
S1_CT_NDT = TypeVar(
"S1_CT_NDT", bound=SeriesDTypeNoDateTime, default=Any, contravariant=True
)
S1_CO = TypeVar("S1_CO", bound=SeriesDType, default=Any, covariant=True)
S1_CT = TypeVar("S1_CT", bound=SeriesDType, default=Any, contravariant=True)
# Like S1, but without `default=Any`.
S2 = TypeVar("S2", bound=SeriesDType)
S2_CT = TypeVar("S2_CT", bound=SeriesDType, contravariant=True)
S2_CO_NSDT = TypeVar("S2_CO_NSDT", bound=SeriesDTypeNoStrDateTime, covariant=True)
S2_CT_NDT = TypeVar("S2_CT_NDT", bound=SeriesDTypeNoDateTime, contravariant=True)
S2_NSDT = TypeVar("S2_NSDT", bound=SeriesDTypeNoStrDateTime)
S3 = TypeVar("S3", bound=SeriesDType)

# Constraint, instead of bound
Expand Down
117 changes: 113 additions & 4 deletions pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@ from typing import (
Any,
Generic,
Literal,
Protocol,
TypeAlias,
final,
overload,
type_check_only,
)

from _typeshed import _T_contra
import numpy as np
from pandas import (
Index,
Series,
)
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays import ExtensionArray
from pandas.core.arrays.categorical import Categorical
from pandas.core.indexes.accessors import ArrayDescriptor
from pandas.core.indexes.base import Index
from pandas.core.series import Series
from typing_extensions import Self

from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._typing import (
S1,
S2,
ArrayLike,
AxisIndex,
DropKeep,
DTypeLike,
GenericT,
GenericT_co,
Just,
NDFrameT,
Scalar,
SequenceNotStr,
Expand Down Expand Up @@ -176,3 +180,108 @@ NumListLike: TypeAlias = (
| Sequence[complex]
| IndexOpsMixin[complex]
)

@type_check_only
class ElementOpsMixin(Generic[S2]):
@overload
def _proto_add(
self: ElementOpsMixin[bool], other: bool | np.bool_
) -> ElementOpsMixin[bool]: ...
@overload
def _proto_add(
self: ElementOpsMixin[int], other: int | np.integer
) -> ElementOpsMixin[int]: ...
@overload
def _proto_add(
self: ElementOpsMixin[float], other: float | np.floating
) -> ElementOpsMixin[float]: ...
@overload
def _proto_add(
self: ElementOpsMixin[complex], other: complex | np.complexfloating
) -> ElementOpsMixin[complex]: ...
@overload
def _proto_add(self: ElementOpsMixin[str], other: str) -> ElementOpsMixin[str]: ...
@overload
def _proto_radd(
self: ElementOpsMixin[bool], other: bool | np.bool_
) -> ElementOpsMixin[bool]: ...
@overload
def _proto_radd(
self: ElementOpsMixin[int], other: int | np.integer
) -> ElementOpsMixin[int]: ...
@overload
def _proto_radd(
self: ElementOpsMixin[float], other: float | np.floating
) -> ElementOpsMixin[float]: ...
@overload
def _proto_radd(
self: ElementOpsMixin[complex], other: complex | np.complexfloating
) -> ElementOpsMixin[complex]: ...
@overload
def _proto_radd(self: ElementOpsMixin[str], other: str) -> ElementOpsMixin[str]: ...
@overload
def _proto_mul(
self: ElementOpsMixin[bool], other: bool | np.bool_
) -> ElementOpsMixin[bool]: ...
@overload
def _proto_mul(
self: ElementOpsMixin[int], other: int | np.integer
) -> ElementOpsMixin[int]: ...
@overload
def _proto_mul(
self: ElementOpsMixin[float], other: float | np.floating
) -> ElementOpsMixin[float]: ...
@overload
def _proto_mul(
self: ElementOpsMixin[complex], other: complex | np.complexfloating
) -> ElementOpsMixin[complex]: ...
@overload
def _proto_mul(
self: ElementOpsMixin[Timedelta],
other: Just[int] | Just[float] | np.integer | np.floating,
) -> ElementOpsMixin[Timedelta]: ...
@overload
def _proto_mul(
self: ElementOpsMixin[str], other: Just[int] | np.integer
) -> ElementOpsMixin[str]: ...
@overload
def _proto_rmul(
self: ElementOpsMixin[bool], other: bool | np.bool_
) -> ElementOpsMixin[bool]: ...
@overload
def _proto_rmul(
self: ElementOpsMixin[int], other: int | np.integer
) -> ElementOpsMixin[int]: ...
@overload
def _proto_rmul(
self: ElementOpsMixin[float], other: float | np.floating
) -> ElementOpsMixin[float]: ...
@overload
def _proto_rmul(
self: ElementOpsMixin[complex], other: complex | np.complexfloating
) -> ElementOpsMixin[complex]: ...
@overload
def _proto_rmul(
self: ElementOpsMixin[Timedelta],
other: Just[int] | Just[float] | np.integer | np.floating,
) -> ElementOpsMixin[Timedelta]: ...
@overload
def _proto_rmul(
self: ElementOpsMixin[str], other: Just[int] | np.integer
) -> ElementOpsMixin[str]: ...

@type_check_only
class Supports_ProtoAdd(Protocol[_T_contra, S2]):
def _proto_add(self, other: _T_contra, /) -> ElementOpsMixin[S2]: ...

@type_check_only
class Supports_ProtoRAdd(Protocol[_T_contra, S2]):
def _proto_radd(self, other: _T_contra, /) -> ElementOpsMixin[S2]: ...

@type_check_only
class Supports_ProtoMul(Protocol[_T_contra, S2]):
def _proto_mul(self, other: _T_contra, /) -> ElementOpsMixin[S2]: ...

@type_check_only
class Supports_ProtoRMul(Protocol[_T_contra, S2]):
def _proto_rmul(self, other: _T_contra, /) -> ElementOpsMixin[S2]: ...
110 changes: 36 additions & 74 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from _typeshed import (
SupportsMul,
SupportsRAdd,
SupportsRMul,
_T_contra,
)
import numpy as np
from pandas import (
Expand All @@ -39,8 +40,13 @@ from pandas import (
TimedeltaIndex,
)
from pandas.core.base import (
ElementOpsMixin,
IndexOpsMixin,
NumListLike,
Supports_ProtoAdd,
Supports_ProtoMul,
Supports_ProtoRAdd,
Supports_ProtoRMul,
_ListLike,
)
from pandas.core.indexes.category import CategoricalIndex
Expand All @@ -55,12 +61,10 @@ from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._typing import (
C2,
S1,
S1_CO,
S1_CT,
S2_CO_NSDT,
S2,
S2_CT,
S2_NSDT,
T_COMPLEX,
T_INT,
AnyAll,
ArrayLike,
AxesData,
Expand Down Expand Up @@ -99,8 +103,8 @@ from pandas._typing import (

class InvalidIndexError(Exception): ...

class Index(IndexOpsMixin[S1]):
__hash__: ClassVar[None] # type: ignore[assignment]
class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]
# overloads with additional dtypes
@overload
def __new__( # pyright: ignore[reportOverlappingOverload]
Expand Down Expand Up @@ -506,20 +510,14 @@ class Index(IndexOpsMixin[S1]):
@overload
def __add__(self, other: Index[Never]) -> Index: ...
@overload
def __add__(self: Index[bool], other: bool | Sequence[bool]) -> Index[bool]: ...
@overload
def __add__(self: Index[int], other: bool | Sequence[bool]) -> Index[int]: ...
@overload
def __add__(self: Index[float], other: int | Sequence[int]) -> Index[float]: ...
@overload
def __add__(
self: Index[complex], other: float | Sequence[float]
) -> Index[complex]: ...
self: Supports_ProtoAdd[_T_contra, S2], other: _T_contra | Sequence[_T_contra]
) -> Index[S2]: ...
@overload
def __add__(
self: Index[S1_CT],
other: SupportsRAdd[S1_CT, S1_CO] | Sequence[SupportsRAdd[S1_CT, S1_CO]],
) -> Index[S1_CO]: ...
self: Index[S2_CT],
other: SupportsRAdd[S2_CT, S2] | Sequence[SupportsRAdd[S2_CT, S2]],
) -> Index[S2]: ...
@overload
def __add__(
self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool]
Expand Down Expand Up @@ -553,27 +551,22 @@ class Index(IndexOpsMixin[S1]):
) -> Never: ...
@overload
def __add__(
self: Index[_str], other: _str | Sequence[_str] | np_ndarray_str | Index[_str]
self: Index[_str], other: np_ndarray_str | Index[_str]
) -> Index[_str]: ...
@overload
def __radd__(self: Index[Never], other: _str) -> Never: ...
@overload
def __radd__(self: Index[Never], other: complex | _ListLike | Index) -> Index: ...
@overload
def __radd__(self: Index[bool], other: bool | Sequence[bool]) -> Index[bool]: ...
@overload
def __radd__(self: Index[int], other: bool | Sequence[bool]) -> Index[int]: ...
@overload
def __radd__(self: Index[float], other: int | Sequence[int]) -> Index[float]: ...
@overload
def __radd__(
self: Index[complex], other: float | Sequence[float]
) -> Index[complex]: ...
self: Supports_ProtoRAdd[_T_contra, S2],
other: _T_contra | Sequence[_T_contra],
) -> Index[S2]: ...
@overload
def __radd__(
self: Index[S1_CT],
other: SupportsAdd[S1_CT, S1_CO] | Sequence[SupportsAdd[S1_CT, S1_CO]],
) -> Index[S1_CO]: ...
self: Index[S2_CT],
other: SupportsAdd[S2_CT, S2] | Sequence[SupportsAdd[S2_CT, S2]],
) -> Index[S2]: ...
@overload
def __radd__(
self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool]
Expand Down Expand Up @@ -607,7 +600,7 @@ class Index(IndexOpsMixin[S1]):
) -> Never: ...
@overload
def __radd__(
self: Index[_str], other: _str | Sequence[_str] | np_ndarray_str | Index[_str]
self: Index[_str], other: np_ndarray_str | Index[_str]
) -> Index[_str]: ...
@overload
def __sub__(self: Index[Never], other: DatetimeIndex) -> Never: ...
Expand Down Expand Up @@ -773,16 +766,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __mul__(
self: Index[Timedelta],
other: (
Just[int]
| Just[float]
| Sequence[Just[int]]
| Sequence[Just[float]]
| np_ndarray_anyint
| np_ndarray_float
| Index[int]
| Index[float]
),
other: np_ndarray_anyint | np_ndarray_float | Index[int] | Index[float],
) -> Index[Timedelta]: ...
@overload
def __mul__(
Expand All @@ -797,24 +781,17 @@ class Index(IndexOpsMixin[S1]):
) -> Never: ...
@overload
def __mul__(
self: Index[_str],
other: Just[int] | Sequence[Just[int]] | np_ndarray_anyint | Index[int],
self: Index[_str], other: np_ndarray_anyint | Index[int]
) -> Index[_str]: ...
@overload
def __mul__(self: Index[T_INT], other: bool | Sequence[bool]) -> Index[T_INT]: ...
@overload
def __mul__(self: Index[float], other: int | Sequence[int]) -> Index[float]: ...
@overload
def __mul__(
self: Index[complex], other: float | Sequence[float]
) -> Index[complex]: ...
self: Supports_ProtoMul[_T_contra, S2], other: _T_contra | Sequence[_T_contra]
) -> Index[S2]: ...
@overload
def __mul__(
self: Index[S2_CT],
other: (
SupportsRMul[S2_CT, S2_CO_NSDT] | Sequence[SupportsRMul[S2_CT, S2_CO_NSDT]]
),
) -> Index[S2_CO_NSDT]: ...
other: SupportsRMul[S2_CT, S2_NSDT] | Sequence[SupportsRMul[S2_CT, S2_NSDT]],
) -> Index[S2_NSDT]: ...
@overload
def __mul__(
self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool]
Expand Down Expand Up @@ -865,16 +842,7 @@ class Index(IndexOpsMixin[S1]):
@overload
def __rmul__(
self: Index[Timedelta],
other: (
Just[int]
| Just[float]
| Sequence[Just[int]]
| Sequence[Just[float]]
| np_ndarray_anyint
| np_ndarray_float
| Index[int]
| Index[float]
),
other: np_ndarray_anyint | np_ndarray_float | Index[int] | Index[float],
) -> Index[Timedelta]: ...
@overload
def __rmul__(
Expand All @@ -889,24 +857,18 @@ class Index(IndexOpsMixin[S1]):
) -> Never: ...
@overload
def __rmul__(
self: Index[_str],
other: Just[int] | Sequence[Just[int]] | np_ndarray_anyint | Index[int],
self: Index[_str], other: np_ndarray_anyint | Index[int]
) -> Index[_str]: ...
@overload
def __rmul__(self: Index[T_INT], other: bool | Sequence[bool]) -> Index[T_INT]: ...
@overload
def __rmul__(self: Index[float], other: int | Sequence[int]) -> Index[float]: ...
@overload
def __rmul__(
self: Index[complex], other: float | Sequence[float]
) -> Index[complex]: ...
self: Supports_ProtoRMul[_T_contra, S2],
other: _T_contra | Sequence[_T_contra],
) -> Index[S2]: ...
@overload
def __rmul__(
self: Index[S2_CT],
other: (
SupportsMul[S2_CT, S2_CO_NSDT] | Sequence[SupportsMul[S2_CT, S2_CO_NSDT]]
),
) -> Index[S2_CO_NSDT]: ...
other: SupportsMul[S2_CT, S2_NSDT] | Sequence[SupportsMul[S2_CT, S2_NSDT]],
) -> Index[S2_NSDT]: ...
@overload
def __rmul__(
self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool]
Expand Down
Loading