Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ ci:
autofix_prs: false
repos:
- repo: https://github.com/python/black
rev: 25.1.0
rev: 25.9.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 6.0.1
hooks:
- id: isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.10
rev: v0.13.1
hooks:
- id: ruff-check
args: [
Expand Down
4 changes: 2 additions & 2 deletions docs/philosophy.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ revealed by `mypy` as follows:
```text
ttest.py:5: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
ttest.py:7: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
ttest.py:9: note: Revealed type is "pandas.core.series.TimedeltaSeries"
ttest.py:9: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timedelta]"
ttest.py:10: error: Unsupported operand types for + ("Series[Timestamp]" and "Series[Timestamp]") [operator]
```

The type `Series[Timestamp]` is the result of creating a series from `pd.to_datetime()`, while
the type `TimedeltaSeries` is the result of subtracting two `Series[Timestamp]` as well as
the type `Series[Timedelta]` is the result of subtracting two `Series[Timestamp]` as well as
the result of `pd.to_timedelta()`.

### Progressive arithmetic typing for generic Series
Expand Down
7 changes: 3 additions & 4 deletions pandas-stubs/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ from pandas import (
Timedelta,
Timestamp,
)
from pandas.core.series import TimedeltaSeries

from pandas._typing import (
IntervalClosedType,
Expand Down Expand Up @@ -173,7 +172,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __gt__(
self,
other: Series[int] | Series[float] | Series[Timestamp] | TimedeltaSeries,
other: Series[int] | Series[float] | Series[Timestamp] | Series[Timedelta],
) -> Series[bool]: ...
@overload
def __lt__(self, other: Interval[_OrderableT]) -> bool: ...
Expand All @@ -184,7 +183,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __lt__(
self,
other: Series[int] | Series[float] | Series[Timestamp] | TimedeltaSeries,
other: Series[int] | Series[float] | Series[Timestamp] | Series[Timedelta],
) -> Series[bool]: ...
@overload
def __ge__(self, other: Interval[_OrderableT]) -> bool: ...
Expand All @@ -195,7 +194,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __ge__(
self,
other: Series[int] | Series[float] | Series[Timestamp] | TimedeltaSeries,
other: Series[int] | Series[float] | Series[Timestamp] | Series[Timedelta],
) -> Series[bool]: ...
@overload
def __le__(self, other: Interval[_OrderableT]) -> bool: ...
Expand Down
7 changes: 3 additions & 4 deletions pandas-stubs/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ from pandas import (
from pandas.core.series import (
OffsetSeries,
PeriodSeries,
TimedeltaSeries,
)
from typing_extensions import TypeAlias

Expand Down Expand Up @@ -86,7 +85,7 @@ class Period(PeriodMixin):
@overload
def __sub__(self, other: PeriodIndex) -> Index: ...
@overload
def __sub__(self, other: TimedeltaSeries) -> PeriodSeries: ...
def __sub__(self, other: Series[Timedelta]) -> PeriodSeries: ...
@overload
def __sub__(self, other: TimedeltaIndex) -> PeriodIndex: ...
@overload
Expand All @@ -96,7 +95,7 @@ class Period(PeriodMixin):
@overload
def __add__(self, other: Index) -> PeriodIndex: ...
@overload
def __add__(self, other: OffsetSeries | TimedeltaSeries) -> PeriodSeries: ...
def __add__(self, other: OffsetSeries | Series[Timedelta]) -> PeriodSeries: ...
# ignore[misc] here because we know all other comparisons
# are False, so we use Literal[False]
@overload
Expand Down Expand Up @@ -172,7 +171,7 @@ class Period(PeriodMixin):
@overload
def __radd__(self, other: Index) -> Index: ...
@overload
def __radd__(self, other: TimedeltaSeries) -> PeriodSeries: ...
def __radd__(self, other: Series[Timedelta]) -> PeriodSeries: ...
@overload
def __radd__(self, other: NaTType) -> NaTType: ...
@property
Expand Down
48 changes: 21 additions & 27 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ from pandas import (
Series,
TimedeltaIndex,
)
from pandas.core.series import TimedeltaSeries
from typing_extensions import (
Self,
TypeAlias,
Expand Down Expand Up @@ -162,10 +161,7 @@ class Timedelta(timedelta):
@overload
def __add__(self, other: pd.TimedeltaIndex) -> pd.TimedeltaIndex: ...
@overload
def __add__(
self,
other: TimedeltaSeries,
) -> TimedeltaSeries: ...
def __add__(self, other: Series[Timedelta]) -> Series[Timedelta]: ...
@overload
def __add__(self, other: Series[Timestamp]) -> Series[Timestamp]: ...
@overload
Expand Down Expand Up @@ -198,9 +194,7 @@ class Timedelta(timedelta):
@overload
def __sub__(self, other: pd.TimedeltaIndex) -> TimedeltaIndex: ...
@overload
def __sub__(
self, other: TimedeltaSeries | Series[pd.Timedelta]
) -> TimedeltaSeries: ...
def __sub__(self, other: Series[pd.Timedelta]) -> Series[pd.Timedelta]: ...
@overload
def __rsub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
@overload
Expand Down Expand Up @@ -234,9 +228,9 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.integer] | np_ndarray[ShapeT, np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __mul__(self, other: Series[int]) -> TimedeltaSeries: ...
def __mul__(self, other: Series[int]) -> Series[Timedelta]: ...
@overload
def __mul__(self, other: Series[float]) -> TimedeltaSeries: ...
def __mul__(self, other: Series[float]) -> Series[Timedelta]: ...
@overload
def __mul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
Expand All @@ -246,9 +240,9 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.floating] | np_ndarray[ShapeT, np.integer]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __rmul__(self, other: Series[int]) -> TimedeltaSeries: ...
def __rmul__(self, other: Series[int]) -> Series[Timedelta]: ...
@overload
def __rmul__(self, other: Series[float]) -> TimedeltaSeries: ...
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: ...
Expand All @@ -269,11 +263,11 @@ class Timedelta(timedelta):
@overload
def __floordiv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __floordiv__(self, other: Series[int]) -> TimedeltaSeries: ...
def __floordiv__(self, other: Series[int]) -> Series[Timedelta]: ...
@overload
def __floordiv__(self, other: Series[float]) -> TimedeltaSeries: ...
def __floordiv__(self, other: Series[float]) -> Series[Timedelta]: ...
@overload
def __floordiv__(self, other: TimedeltaSeries) -> Series[int]: ...
def __floordiv__(self, other: Series[Timedelta]) -> Series[int]: ...
@overload
def __floordiv__(self, other: NaTType | None) -> float: ...
@overload
Expand All @@ -294,19 +288,19 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.integer] | np_ndarray[ShapeT, np.floating]
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __truediv__(self, other: TimedeltaSeries) -> Series[float]: ...
def __truediv__(self, other: Series[Timedelta]) -> Series[float]: ...
@overload
def __truediv__(self, other: Series[int]) -> TimedeltaSeries: ...
def __truediv__(self, other: Series[int]) -> Series[Timedelta]: ...
@overload
def __truediv__(self, other: Series[float]) -> TimedeltaSeries: ...
def __truediv__(self, other: Series[float]) -> Series[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
@overload
def __eq__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __eq__(self, other: TimedeltaSeries | Series[pd.Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
def __eq__(self, other: Series[pd.Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
@overload
def __eq__(self, other: Index) -> np_1darray[np.bool]: ... # type: ignore[overload-overlap]
@overload
Expand All @@ -319,7 +313,7 @@ class Timedelta(timedelta):
@overload
def __ne__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __ne__(self, other: TimedeltaSeries | Series[pd.Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
def __ne__(self, other: Series[pd.Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
@overload
def __ne__(self, other: Index) -> np_1darray[np.bool]: ... # type: ignore[overload-overlap]
@overload
Expand All @@ -334,7 +328,7 @@ class Timedelta(timedelta):
@overload
def __mod__(self, other: float) -> Timedelta: ...
@overload
def __mod__(self, other: Series[int] | Series[float]) -> TimedeltaSeries: ...
def __mod__(self, other: Series[int] | Series[float]) -> Series[Timedelta]: ...
@overload
def __mod__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
Expand All @@ -343,8 +337,8 @@ class Timedelta(timedelta):
) -> np_ndarray[ShapeT, np.timedelta64]: ...
@overload
def __mod__(
self, other: Series[int] | Series[float] | TimedeltaSeries
) -> TimedeltaSeries: ...
self, other: Series[int] | Series[float] | Series[Timedelta]
) -> Series[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
Expand All @@ -358,7 +352,7 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.timedelta64]
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __le__(self, other: TimedeltaSeries | Series[pd.Timedelta]) -> Series[bool]: ...
def __le__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __lt__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
Expand All @@ -369,7 +363,7 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.timedelta64]
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __lt__(self, other: TimedeltaSeries | Series[pd.Timedelta]) -> Series[bool]: ...
def __lt__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __ge__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
Expand All @@ -380,7 +374,7 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.timedelta64]
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __ge__(self, other: TimedeltaSeries | Series[pd.Timedelta]) -> Series[bool]: ...
def __ge__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __gt__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
Expand All @@ -391,7 +385,7 @@ class Timedelta(timedelta):
self, other: np_ndarray[ShapeT, np.timedelta64]
) -> np_ndarray[ShapeT, np.bool_]: ...
@overload
def __gt__(self, other: TimedeltaSeries | Series[pd.Timedelta]) -> Series[bool]: ...
def __gt__(self, other: Series[pd.Timedelta]) -> Series[bool]: ...
def __hash__(self) -> int: ...
def isoformat(self) -> str: ...
def to_numpy(self) -> np.timedelta64: ...
Expand Down
9 changes: 1 addition & 8 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ from pandas import (
TimedeltaIndex,
)
from pandas.core.indexes.base import Index
from pandas.core.series import (
Series,
TimedeltaSeries,
)
from pandas.core.series import Series
from typing_extensions import (
Never,
Self,
Expand Down Expand Up @@ -225,8 +222,6 @@ class Timestamp(datetime, SupportsIndex):
@overload
def __add__(self, other: timedelta | np.timedelta64 | Tick) -> Self: ...
@overload
def __add__(self, other: TimedeltaSeries) -> Series[Timestamp]: ...
@overload
def __add__(self, other: TimedeltaIndex) -> DatetimeIndex: ...
@overload
def __radd__(self, other: timedelta) -> Self: ...
Expand All @@ -244,8 +239,6 @@ class Timestamp(datetime, SupportsIndex):
@overload
def __sub__(self, other: TimedeltaIndex) -> DatetimeIndex: ...
@overload
def __sub__(self, other: TimedeltaSeries) -> Series[Timestamp]: ...
@overload
def __sub__(
self, other: np_ndarray[ShapeT, np.timedelta64]
) -> np_ndarray[ShapeT, np.datetime64]: ...
Expand Down
8 changes: 8 additions & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,14 @@ np_ndarray_td: TypeAlias = npt.NDArray[np.timedelta64]

# Define shape and generic type variables with defaults similar to numpy
GenericT = TypeVar("GenericT", bound=np.generic, default=Any)
TD64ItemT = TypeVar(
"TD64ItemT",
bound=datetime.timedelta | int | None,
default=datetime.timedelta | int | None,
)
DT64ItemT = TypeVar(
"DT64ItemT", bound=datetime.date | int | None, default=datetime.date | int | None
)
GenericT_co = TypeVar("GenericT_co", bound=np.generic, default=Any, covariant=True)
ShapeT = TypeVar("ShapeT", bound=tuple[int, ...], default=tuple[Any, ...])
# Numpy ndarray with more ergonomic typevar
Expand Down
8 changes: 0 additions & 8 deletions pandas-stubs/core/arraylike.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ class OpsMixin:
def __rxor__(self, other: Any) -> Self: ...
# -------------------------------------------------------------
# Arithmetic Methods
def __mul__(self, other: Any) -> Self: ...
def __rmul__(self, other: Any) -> Self: ...
# Handled by subclasses that specify only the valid values
# that can be passed
# def __truediv__(self, other: Any) -> Self: ...
# def __rtruediv__(self, other: Any) -> Self: ...
# def __floordiv__(self, other: Any) -> Self: ...
# def __rfloordiv__(self, other: Any) -> Self: ...
def __mod__(self, other: Any) -> Self: ...
def __rmod__(self, other: Any) -> Self: ...
def __divmod__(self, other: Any) -> tuple[Self, Self]: ...
Expand Down
4 changes: 3 additions & 1 deletion pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
@overload
def to_xml(
self,
path_or_buffer: Literal[None] = ...,
path_or_buffer: None = ...,
index: bool = ...,
root_name: str | None = ...,
row_name: str | None = ...,
Expand Down Expand Up @@ -1806,6 +1806,8 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
level: Level | None = ...,
fill_value: float | None = None,
) -> Self: ...
def __mul__(self, other: Any) -> Self: ...
def __rmul__(self, other: Any) -> Self: ...
@final
def add_prefix(self, prefix: _str, axis: Axis | None = None) -> Self: ...
@final
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/groupby/indexing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ class GroupByNthSelector(Generic[_GroupByT]):
def __call__(
self,
n: PositionalIndexer | tuple,
dropna: Literal["any", "all", None] = ...,
dropna: Literal["any", "all"] | None = ...,
) -> DataFrame | Series: ...
def __getitem__(self, n: PositionalIndexer | tuple) -> DataFrame | Series: ...
11 changes: 7 additions & 4 deletions pandas-stubs/core/indexes/accessors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ from pandas.core.frame import DataFrame
from pandas.core.series import (
PeriodSeries,
Series,
TimedeltaSeries,
)
from typing_extensions import Never

Expand Down Expand Up @@ -165,7 +164,11 @@ class _DatetimeLikeOps(

_DTTimestampTimedeltaReturnType = TypeVar(
"_DTTimestampTimedeltaReturnType",
bound=Series | Series[Timestamp] | TimedeltaSeries | DatetimeIndex | TimedeltaIndex,
bound=Series
| Series[Timestamp]
| Series[Timedelta]
| DatetimeIndex
| TimedeltaIndex,
)

class _DatetimeRoundingMethods(Generic[_DTTimestampTimedeltaReturnType]):
Expand Down Expand Up @@ -316,7 +319,7 @@ class _TimedeltaPropertiesNoRounding(
class TimedeltaProperties(
Properties,
_TimedeltaPropertiesNoRounding[Series[int], Series[float]],
_DatetimeRoundingMethods[TimedeltaSeries],
_DatetimeRoundingMethods[Series[Timedelta]],
):
@property
def unit(self) -> TimeUnit: ...
Expand Down Expand Up @@ -440,7 +443,7 @@ class _dtDescriptor(CombinedDatetimelikeProperties, Generic[S1]):
) -> TimestampProperties: ...
@overload
def __get__(
self, instance: Series[Timedelta] | TimedeltaSeries, owner: Any
self, instance: Series[Timedelta], owner: Any
) -> TimedeltaProperties: ...
@overload
def __get__(
Expand Down
Loading
Loading