Skip to content
Closed
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
33 changes: 16 additions & 17 deletions pandas-stubs/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ from pandas import (
Timedelta,
TimedeltaIndex,
)
from typing_extensions import TypeAlias
from typing_extensions import (
Self,
TypeAlias,
)

from pandas._libs.tslibs import NaTType
from pandas._libs.tslibs.offsets import BaseOffset
Expand Down Expand Up @@ -87,15 +90,23 @@ class Period(PeriodMixin):
@overload
def __sub__(self, other: TimedeltaIndex) -> PeriodIndex: ...
@overload
def __add__(self, other: _PeriodAddSub) -> Period: ...
def __add__(self, other: _PeriodAddSub) -> Self: ...
@overload
def __add__(self, other: NaTType) -> NaTType: ...
@overload
def __add__(self, other: Index) -> PeriodIndex: ...
# Ignored due to indecipherable error from mypy:
# Forward operator "__add__" is not callable [misc]
@overload
def __add__(
self, other: Series[BaseOffset] | Series[Timedelta]
) -> Series[Period]: ... # pyrefly: ignore[bad-specialization]
def __radd__(self, other: _PeriodAddSub) -> Self: ... # type: ignore[misc]
@overload
def __radd__(self, other: NaTType) -> NaTType: ...
# Real signature is -> PeriodIndex, but conflicts with Index.__add__
# Changing Index is very hard due to Index inheritance
# Signatures of "__radd__" of "Period" and "__add__" of "Index"
# are unsafely overlapping
@overload
def __radd__(self, other: Index) -> PeriodIndex: ...
# ignore[misc] here because we know all other comparisons
# are False, so we use Literal[False]
@overload
Expand Down Expand Up @@ -168,18 +179,6 @@ class Period(PeriodMixin):
def __ne__(self, other: np_ndarray[ShapeT, np.object_]) -> np_ndarray[ShapeT, np.bool]: ... # type: ignore[overload-overlap]
@overload
def __ne__(self, other: object) -> Literal[True]: ...
# Ignored due to indecipherable error from mypy:
# Forward operator "__add__" is not callable [misc]
@overload
def __radd__(self, other: _PeriodAddSub) -> Period: ... # type: ignore[misc]
# Real signature is -> PeriodIndex, but conflicts with Index.__add__
# Changing Index is very hard due to Index inheritance
# Signatures of "__radd__" of "Period" and "__add__" of "Index"
# are unsafely overlapping
@overload
def __radd__(self, other: Index) -> Index: ...
@overload
def __radd__(self, other: NaTType) -> NaTType: ...
@property
def day(self) -> int: ...
@property
Expand Down
88 changes: 38 additions & 50 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# pyright: strict
import datetime as dt
from datetime import timedelta
from datetime import (
date,
datetime,
timedelta,
)
from typing import (
ClassVar,
Literal,
Expand Down Expand Up @@ -135,17 +138,17 @@ class Timedelta(timedelta):
def ceil(self, freq: str | BaseOffset) -> Self: ...
@property
def resolution_string(self) -> str: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __add__(self, other: dt.datetime | np.datetime64) -> Timestamp: ...
def __add__(self, other: datetime | np.datetime64) -> Timestamp: ...
@overload
def __add__(self, other: timedelta | np.timedelta64) -> Self: ...
@overload
def __add__(self, other: NaTType) -> NaTType: ...
@overload
def __add__(self, other: Period) -> Period: ...
@overload
def __add__(self, other: dt.date) -> dt.date: ...
def __add__(self, other: date) -> date: ...
@overload
def __add__(
self, other: np_ndarray[ShapeT, np.timedelta64]
Expand All @@ -155,13 +158,13 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.datetime64]
) -> np_ndarray[ShapeT, np.datetime64]: ...
@overload
def __radd__(self, other: dt.datetime | np.datetime64) -> Timestamp: ... # type: ignore[misc]
def __radd__(self, other: datetime | np.datetime64) -> Timestamp: ... # type: ignore[misc]
@overload
def __radd__(self, other: timedelta | np.timedelta64) -> Self: ...
@overload
def __radd__(self, other: NaTType) -> NaTType: ...
@overload
def __radd__(self, other: dt.date) -> dt.date: ...
def __radd__(self, other: date) -> date: ...
@overload
def __radd__(
self, other: np_ndarray[ShapeT, np.timedelta64]
Expand All @@ -170,9 +173,9 @@ class Timedelta(timedelta):
def __radd__(
self, other: np_ndarray[ShapeT, np.datetime64]
) -> np_ndarray[ShapeT, np.datetime64]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __sub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
def __sub__(self, other: timedelta | Timedelta | np.timedelta64) -> Self: ...
@overload
def __sub__(self, other: NaTType) -> NaTType: ...
@overload
Expand All @@ -182,11 +185,9 @@ class Timedelta(timedelta):
@overload
def __sub__(self, other: pd.TimedeltaIndex) -> TimedeltaIndex: ...
@overload
def __sub__(self, other: Series[pd.Timedelta]) -> Series[pd.Timedelta]: ...
@overload
def __rsub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
def __rsub__(self, other: timedelta | Timedelta | np.timedelta64) -> Self: ...
@overload
def __rsub__(self, other: dt.datetime | Timestamp | np.datetime64) -> Timestamp: ... # type: ignore[misc]
def __rsub__(self, other: datetime | Timestamp | np.datetime64) -> Timestamp: ... # type: ignore[misc]
@overload
def __rsub__(self, other: NaTType) -> NaTType: ...
@overload
Expand All @@ -205,44 +206,31 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __rsub__(self, other: pd.TimedeltaIndex) -> pd.TimedeltaIndex: ...
def __neg__(self) -> Timedelta: ...
def __pos__(self) -> Timedelta: ...
def __abs__(self) -> Timedelta: ...
# Override due to more types supported than dt.timedelta
def __neg__(self) -> Self: ...
def __pos__(self) -> Self: ...
def __abs__(self) -> Self: ...
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __mul__(self, other: float) -> Timedelta: ...
def __mul__(self, other: float) -> Self: ...
@overload
def __mul__(
self, other: np_ndarray[ShapeT, np.integer] | np_ndarray[ShapeT, np.floating]
self, other: np_ndarray[ShapeT, np.bool_ | np.integer | np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __mul__(self, other: Series[int]) -> Series[Timedelta]: ...
@overload
def __mul__(self, other: Series[float]) -> Series[Timedelta]: ...
@overload
def __mul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __rmul__(self, other: float) -> Timedelta: ...
def __rmul__(self, other: float) -> Self: ...
@overload
def __rmul__(
self, other: np_ndarray[ShapeT, np.floating] | np_ndarray[ShapeT, np.integer]
self, other: np_ndarray[ShapeT, np.bool_ | np.integer | np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __rmul__(self, other: Series[int]) -> Series[Timedelta]: ...
@overload
def __rmul__(self, other: Series[float]) -> Series[Timedelta]: ...
# maybe related to https://github.com/python/mypy/issues/10755
@overload
def __rmul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
# error: Signature of "__floordiv__" incompatible with supertype "timedelta"
@overload # type: ignore[override]
def __floordiv__(self, other: timedelta | Timedelta | np.timedelta64) -> int: ...
@overload
def __floordiv__(self, other: float) -> Timedelta: ...
def __floordiv__(self, other: float) -> Self: ...
@overload
def __floordiv__(
self, other: np_ndarray[ShapeT, np.integer] | np_ndarray[ShapeT, np.floating]
self, other: np_ndarray[ShapeT, np.integer | np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __floordiv__(
Expand All @@ -266,14 +254,14 @@ class Timedelta(timedelta):
def __rfloordiv__(
self, other: np_ndarray[ShapeT, np.timedelta64]
) -> np_ndarray[ShapeT, np.int_]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __truediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
@overload
def __truediv__(self, other: float) -> Timedelta: ...
def __truediv__(self, other: float) -> Self: ...
@overload
def __truediv__(
self, other: np_ndarray[ShapeT, np.integer] | np_ndarray[ShapeT, np.floating]
self, other: np_ndarray[ShapeT, np.integer | np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __truediv__(self, other: Series[Timedelta]) -> Series[float]: ...
Expand All @@ -284,7 +272,7 @@ class Timedelta(timedelta):
@overload
def __truediv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
def __rtruediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload
def __eq__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
Expand All @@ -297,7 +285,7 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __eq__(self, other: object) -> Literal[False]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload
def __ne__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
Expand All @@ -310,18 +298,18 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __ne__(self, other: object) -> Literal[True]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __mod__(self, other: timedelta) -> Timedelta: ...
def __mod__(self, other: timedelta) -> Self: ...
@overload
def __mod__(self, other: float) -> Timedelta: ...
def __mod__(self, other: float) -> Self: ...
@overload
def __mod__(self, other: Series[int] | Series[float]) -> Series[Timedelta]: ...
@overload
def __mod__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __mod__(
self, other: np_ndarray[ShapeT, np.integer] | np_ndarray[ShapeT, np.floating]
self, other: np_ndarray[ShapeT, np.integer | np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __mod__(
Expand All @@ -330,7 +318,7 @@ class Timedelta(timedelta):
def __divmod__(self, other: timedelta) -> tuple[int, Timedelta]: ...
# Mypy complains Forward operator "<inequality op>" is not callable, so ignore misc
# for le, lt ge and gt
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __le__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
Expand All @@ -341,7 +329,7 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __le__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __lt__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
Expand All @@ -352,7 +340,7 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __lt__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __ge__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
Expand All @@ -363,7 +351,7 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __ge__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
# Override due to more types supported than dt.timedelta
# Override due to more types supported than timedelta
@overload # type: ignore[override]
def __gt__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
Expand Down
18 changes: 12 additions & 6 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ RandomState: TypeAlias = (
)

# dtypes
NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object]
NpDtypeNoStr: TypeAlias = np.dtype[np.generic] | type[complex | bool | object]
NpDtype: TypeAlias = str | NpDtypeNoStr | type[str]
Dtype: TypeAlias = ExtensionDtype | NpDtype

# AstypeArg is more carefully defined here as compared to pandas
Expand Down Expand Up @@ -847,19 +848,21 @@ MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool]

T_INT = TypeVar("T_INT", bound=int)
T_COMPLEX = TypeVar("T_COMPLEX", bound=complex)
SeriesDTypeNoDateTime: TypeAlias = (
str
| bytes
SeriesDTypeNoStrDateTime: TypeAlias = (
bytes
| bool
| int
| float
| complex
| Dtype
| NpDtypeNoStr
| ExtensionDtype
| Period
| Interval
| CategoricalDtype
| BaseOffset
| list[str]
)
SeriesDTypeNoDateTime: TypeAlias = (
str | SeriesDTypeNoStrDateTime | type[str] | list[str]
)
SeriesDType: TypeAlias = (
SeriesDTypeNoDateTime
Expand All @@ -869,6 +872,9 @@ SeriesDType: TypeAlias = (
| datetime.timedelta # includes pd.Timedelta
)
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
S1_CO_NSDT = TypeVar(
"S1_CO_NSDT", bound=SeriesDTypeNoStrDateTime, default=Any, covariant=True
)
S1_CT_NDT = TypeVar(
"S1_CT_NDT", bound=SeriesDTypeNoDateTime, default=Any, contravariant=True
)
Expand Down
Loading