Skip to content

refactor: #718 also drop TimedeltaSeries #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 11 additions & 7 deletions docs/philosophy.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ This also allows type checking for operations on series that contain date/time d
the following example that creates two series of datetimes with corresponding arithmetic.

```python
import pandas as pd

s1 = pd.Series(pd.to_datetime(["2022-05-01", "2022-06-01"]))
reveal_type(s1)
s2 = pd.Series(pd.to_datetime(["2022-05-15", "2022-06-15"]))
Expand All @@ -46,19 +48,21 @@ ssum = s1 + s2
reveal_type(ssum)
```

The above code (without the `reveal_type()` statements) will raise an `Exception` on the computation of `ssum` because it is
The above code (without the `reveal_type()` statements) will get a `Never`
on the computation of `ssum` because it is
inappropriate to add two series containing `Timestamp` values. The types will be
revealed as follows:

```text
ttest.py:4: note: Revealed type is "pandas.core.series.TimestampSeries"
ttest.py:6: note: Revealed type is "pandas.core.series.TimestampSeries"
ttest.py:8: note: Revealed type is "pandas.core.series.TimedeltaSeries"
ttest.py:10: note: Revealed type is "builtins.Exception"
ttest.py:4: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
ttest.py:6: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timestamps.Timestamp]"
ttest.py:8: note: Revealed type is "pandas.core.series.Series[pandas._libs.tslibs.timedeltas.Timedelta]"
ttest.py:9: error: Need type annotation for "ssum" [var-annotated]
ttest.py:10: note: Revealed type is "Never"
```

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

### Interval is Generic
Expand Down
10 changes: 3 additions & 7 deletions pandas-stubs/_libs/interval.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ from pandas import (
Timedelta,
Timestamp,
)
from pandas.core.series import (
TimedeltaSeries,
TimestampSeries,
)

from pandas._typing import (
IntervalClosedType,
Expand Down Expand Up @@ -174,7 +170,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __gt__(
self,
other: Series[int] | Series[float] | TimestampSeries | TimedeltaSeries,
other: Series[int] | Series[float] | Series[Timestamp] | Series[Timedelta],
) -> Series[bool]: ...
@overload
def __lt__(self, other: Interval[_OrderableT]) -> bool: ...
Expand All @@ -183,7 +179,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __lt__(
self,
other: Series[int] | Series[float] | TimestampSeries | TimedeltaSeries,
other: Series[int] | Series[float] | Series[Timestamp] | Series[Timedelta],
) -> Series[bool]: ...
@overload
def __ge__(self, other: Interval[_OrderableT]) -> bool: ...
Expand All @@ -192,7 +188,7 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
@overload
def __ge__(
self,
other: Series[int] | Series[float] | TimestampSeries | 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 @@ -82,7 +81,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 @@ -92,7 +91,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 @@ -148,7 +147,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
53 changes: 22 additions & 31 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ from pandas import (
Series,
TimedeltaIndex,
)
from pandas.core.series import (
TimedeltaSeries,
TimestampSeries,
)
from typing_extensions import (
Self,
TypeAlias,
Expand Down Expand Up @@ -162,12 +158,9 @@ 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: TimestampSeries) -> TimestampSeries: ...
def __add__(self, other: Series[Timestamp]) -> Series[Timestamp]: ...
@overload
def __radd__(self, other: np.datetime64) -> Timestamp: ...
@overload
Expand Down Expand Up @@ -198,9 +191,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 +225,9 @@ class Timedelta(timedelta):
self, other: npt.NDArray[np.integer] | npt.NDArray[np.floating]
) -> npt.NDArray[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 +237,9 @@ class Timedelta(timedelta):
self, other: npt.NDArray[np.floating] | npt.NDArray[np.integer]
) -> npt.NDArray[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 +260,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 +285,19 @@ class Timedelta(timedelta):
self, other: npt.NDArray[np.integer] | npt.NDArray[np.floating]
) -> npt.NDArray[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[Timedelta]) -> Series[bool]: ... # type: ignore[overload-overlap]
@overload
def __eq__( # type: ignore[overload-overlap]
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
Expand All @@ -317,7 +308,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__( # type: ignore[overload-overlap]
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
Expand All @@ -330,7 +321,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 @@ -339,8 +330,8 @@ class Timedelta(timedelta):
) -> npt.NDArray[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 @@ -352,7 +343,7 @@ class Timedelta(timedelta):
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
) -> npt.NDArray[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 @@ -361,7 +352,7 @@ class Timedelta(timedelta):
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
) -> npt.NDArray[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 @@ -370,7 +361,7 @@ class Timedelta(timedelta):
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
) -> npt.NDArray[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 @@ -379,7 +370,7 @@ class Timedelta(timedelta):
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
) -> npt.NDArray[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
24 changes: 10 additions & 14 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ from pandas import (
Index,
TimedeltaIndex,
)
from pandas.core.series import (
Series,
TimedeltaSeries,
TimestampSeries,
)
from pandas.core.series import Series
from typing_extensions import (
Never,
Self,
Expand Down Expand Up @@ -172,31 +168,31 @@ class Timestamp(datetime, SupportsIndex):
self, other: DatetimeIndex | npt.NDArray[np.datetime64]
) -> np_ndarray_bool: ...
@overload
def __le__(self, other: TimestampSeries) -> Series[bool]: ...
def __le__(self, other: Series[Timestamp]) -> Series[bool]: ...
@overload # type: ignore[override]
def __lt__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __lt__(
self, other: DatetimeIndex | npt.NDArray[np.datetime64]
) -> np_ndarray_bool: ...
@overload
def __lt__(self, other: TimestampSeries) -> Series[bool]: ...
def __lt__(self, other: Series[Timestamp]) -> Series[bool]: ...
@overload # type: ignore[override]
def __ge__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __ge__(
self, other: DatetimeIndex | npt.NDArray[np.datetime64]
) -> np_ndarray_bool: ...
@overload
def __ge__(self, other: TimestampSeries) -> Series[bool]: ...
def __ge__(self, other: Series[Timestamp]) -> Series[bool]: ...
@overload # type: ignore[override]
def __gt__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __gt__(
self, other: DatetimeIndex | npt.NDArray[np.datetime64]
) -> np_ndarray_bool: ...
@overload
def __gt__(self, other: TimestampSeries) -> Series[bool]: ...
def __gt__(self, other: Series[Timestamp]) -> Series[bool]: ...
# error: Signature of "__add__" incompatible with supertype "date"/"datetime"
@overload # type: ignore[override]
def __add__(
Expand All @@ -205,7 +201,7 @@ class Timestamp(datetime, SupportsIndex):
@overload
def __add__(self, other: timedelta | np.timedelta64 | Tick) -> Self: ...
@overload
def __add__(self, other: TimedeltaSeries) -> TimestampSeries: ...
def __add__(self, other: Series[Timedelta]) -> Series[Timestamp]: ...
@overload
def __add__(self, other: TimedeltaIndex) -> DatetimeIndex: ...
@overload
Expand All @@ -224,25 +220,25 @@ class Timestamp(datetime, SupportsIndex):
@overload
def __sub__(self, other: TimedeltaIndex) -> DatetimeIndex: ...
@overload
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
def __sub__(self, other: Series[Timedelta]) -> Series[Timestamp]: ...
@overload
def __sub__(self, other: TimestampSeries) -> TimedeltaSeries: ...
def __sub__(self, other: Series[Timestamp]) -> Series[Timedelta]: ...
@overload
def __sub__(
self, other: npt.NDArray[np.timedelta64]
) -> npt.NDArray[np.datetime64]: ...
@overload
def __eq__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __eq__(self, other: TimestampSeries) -> Series[bool]: ... # type: ignore[overload-overlap]
def __eq__(self, other: Series[Timestamp]) -> Series[bool]: ... # type: ignore[overload-overlap]
@overload
def __eq__(self, other: npt.NDArray[np.datetime64] | Index) -> np_ndarray_bool: ... # type: ignore[overload-overlap]
@overload
def __eq__(self, other: object) -> Literal[False]: ...
@overload
def __ne__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __ne__(self, other: TimestampSeries) -> Series[bool]: ... # type: ignore[overload-overlap]
def __ne__(self, other: Series[Timestamp]) -> Series[bool]: ... # type: ignore[overload-overlap]
@overload
def __ne__(self, other: npt.NDArray[np.datetime64] | Index) -> np_ndarray_bool: ... # type: ignore[overload-overlap]
@overload
Expand Down
Loading
Loading