Skip to content

Timestamp -> datetime and Timedelta -> timedelta #841

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

Merged
merged 5 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions pandas-stubs/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ from pandas import (
Index,
PeriodIndex,
Series,
Timedelta,
TimedeltaIndex,
)
from pandas.core.series import (
Expand All @@ -27,7 +26,7 @@ from pandas._typing import npt
class IncompatibleFrequency(ValueError): ...

_PeriodAddSub: TypeAlias = (
Timedelta | datetime.timedelta | np.timedelta64 | np.int64 | int | BaseOffset
datetime.timedelta | np.timedelta64 | np.int64 | int | BaseOffset
)

_PeriodFreqHow: TypeAlias = Literal[
Expand Down
35 changes: 18 additions & 17 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Timedelta(timedelta):
value: int
def __new__(
cls,
value: str | int | Timedelta | timedelta | np.timedelta64 = ...,
value: str | int | timedelta | np.timedelta64 = ...,
unit: TimeDeltaUnitChoices = ...,
*,
days: float | np.integer | np.floating = ...,
Expand Down Expand Up @@ -144,9 +144,9 @@ class Timedelta(timedelta):
def resolution_string(self) -> str: ...
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __add__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
def __add__(self, other: timedelta | np.timedelta64) -> Timedelta: ...
@overload
def __add__(self, other: dt.datetime | np.datetime64 | Timestamp) -> Timestamp: ...
def __add__(self, other: dt.datetime | np.datetime64) -> Timestamp: ...
@overload
def __add__(self, other: NaTType) -> NaTType: ...
@overload
Expand Down Expand Up @@ -177,7 +177,7 @@ class Timedelta(timedelta):
@overload
def __radd__(self, other: np.datetime64) -> Timestamp: ...
@overload
def __radd__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
def __radd__(self, other: timedelta | np.timedelta64) -> Timedelta: ...
@overload
def __radd__(self, other: NaTType) -> NaTType: ...
@overload
Expand All @@ -194,7 +194,7 @@ class Timedelta(timedelta):
def __radd__(self, other: pd.PeriodIndex) -> pd.PeriodIndex: ...
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __sub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
def __sub__(self, other: timedelta | np.timedelta64) -> Timedelta: ...
@overload
def __sub__(self, other: NaTType) -> NaTType: ...
@overload
Expand All @@ -208,9 +208,10 @@ class Timedelta(timedelta):
self, other: TimedeltaSeries | Series[pd.Timedelta]
) -> TimedeltaSeries: ...
@overload
def __rsub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
def __rsub__(self, other: timedelta | np.timedelta64) -> Timedelta: ...
# Timedelta - Timestamp is not allowed but the reverse is
@overload
def __rsub__(self, other: Timestamp | np.datetime64) -> Timestamp: ...
def __rsub__(self, other: dt.datetime | np.datetime64) -> Timestamp: ... # type: ignore[misc]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timestamp

@overload
def __rsub__(self, other: NaTType) -> NaTType: ...
@overload
Expand Down Expand Up @@ -259,7 +260,7 @@ class Timedelta(timedelta):
# Override due to more types supported than dt.timedelta
# error: Signature of "__floordiv__" incompatible with supertype "timedelta"
@overload # type: ignore[override]
def __floordiv__(self, other: timedelta | Timedelta | np.timedelta64) -> int: ...
def __floordiv__(self, other: timedelta | np.timedelta64) -> int: ...
@overload
def __floordiv__(self, other: float) -> Timedelta: ...
@overload
Expand All @@ -281,7 +282,7 @@ class Timedelta(timedelta):
@overload
def __floordiv__(self, other: NaTType | None) -> float: ...
@overload
def __rfloordiv__(self, other: timedelta | Timedelta | str) -> int: ...
def __rfloordiv__(self, other: timedelta | str) -> int: ...
@overload
def __rfloordiv__(self, other: NaTType | None) -> float: ...
@overload
Expand All @@ -290,7 +291,7 @@ class Timedelta(timedelta):
) -> npt.NDArray[np.int_]: ...
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __truediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
def __truediv__(self, other: timedelta | NaTType) -> float: ...
@overload
def __truediv__(self, other: float) -> Timedelta: ...
@overload
Expand All @@ -305,10 +306,10 @@ class Timedelta(timedelta):
def __truediv__(self, other: Series[float]) -> TimedeltaSeries: ...
@overload
def __truediv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
def __rtruediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
def __rtruediv__(self, other: 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]
def __eq__(self, other: 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]
@overload
Expand All @@ -319,7 +320,7 @@ class Timedelta(timedelta):
def __eq__(self, other: object) -> Literal[False]: ...
# Override due to more types supported than dt.timedelta
@overload
def __ne__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
def __ne__(self, other: 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]
@overload
Expand Down Expand Up @@ -350,7 +351,7 @@ class Timedelta(timedelta):
# for le, lt ge and gt
# Override due to more types supported than dt.timedelta
@overload # type: ignore[override]
def __le__(self, other: timedelta | Timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
def __le__(self, other: timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
def __le__(
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
Expand All @@ -359,7 +360,7 @@ class Timedelta(timedelta):
def __le__(self, other: TimedeltaSeries | 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]
def __lt__(self, other: timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
def __lt__(
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
Expand All @@ -368,7 +369,7 @@ class Timedelta(timedelta):
def __lt__(self, other: TimedeltaSeries | 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]
def __ge__(self, other: timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
def __ge__(
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
Expand All @@ -377,7 +378,7 @@ class Timedelta(timedelta):
def __ge__(self, other: TimedeltaSeries | 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]
def __gt__(self, other: timedelta | np.timedelta64) -> bool: ... # type: ignore[misc]
@overload
def __gt__(
self, other: TimedeltaIndex | npt.NDArray[np.timedelta64]
Expand Down
16 changes: 8 additions & 8 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ from pandas._typing import (

_Ambiguous: TypeAlias = bool | Literal["raise", "NaT"]
_Nonexistent: TypeAlias = (
Literal["raise", "NaT", "shift_backward", "shift_forward"] | Timedelta | timedelta
Literal["raise", "NaT", "shift_backward", "shift_forward"] | timedelta
)

class Timestamp(datetime):
Expand Down Expand Up @@ -157,25 +157,25 @@ class Timestamp(datetime):
# Mypy complains Forward operator "<inequality op>" is not callable, so ignore misc
# for le, lt ge and gt
@overload # type: ignore[override]
def __le__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
def __le__(self, other: datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __le__(self, other: Index | npt.NDArray[np.datetime64]) -> np_ndarray_bool: ...
@overload
def __le__(self, other: TimestampSeries) -> Series[bool]: ...
@overload # type: ignore[override]
def __lt__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
def __lt__(self, other: datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __lt__(self, other: Index | npt.NDArray[np.datetime64]) -> np_ndarray_bool: ...
@overload
def __lt__(self, other: TimestampSeries) -> Series[bool]: ...
@overload # type: ignore[override]
def __ge__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
def __ge__(self, other: datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __ge__(self, other: Index | npt.NDArray[np.datetime64]) -> np_ndarray_bool: ...
@overload
def __ge__(self, other: TimestampSeries) -> Series[bool]: ...
@overload # type: ignore[override]
def __gt__(self, other: Timestamp | datetime | np.datetime64) -> bool: ... # type: ignore[misc]
def __gt__(self, other: datetime | np.datetime64) -> bool: ... # type: ignore[misc]
@overload
def __gt__(self, other: Index | npt.NDArray[np.datetime64]) -> np_ndarray_bool: ...
@overload
Expand All @@ -201,7 +201,7 @@ class Timestamp(datetime):
) -> npt.NDArray[np.datetime64]: ...
# TODO: test dt64
@overload # type: ignore[override]
def __sub__(self, other: Timestamp | datetime | np.datetime64) -> Timedelta: ...
def __sub__(self, other: datetime | np.datetime64) -> Timedelta: ...
@overload
def __sub__(self, other: timedelta | np.timedelta64 | Tick) -> Self: ...
@overload
Expand All @@ -213,15 +213,15 @@ class Timestamp(datetime):
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]
def __eq__(self, other: datetime | np.datetime64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __eq__(self, other: TimestampSeries) -> 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]
def __ne__(self, other: datetime | np.datetime64) -> bool: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
@overload
def __ne__(self, other: TimestampSeries) -> Series[bool]: ... # type: ignore[overload-overlap]
@overload
Expand Down
21 changes: 10 additions & 11 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ from collections.abc import (
MutableMapping,
Sequence,
)
import datetime
import datetime as _dt
import datetime as dt
from re import Pattern
from typing import (
Any,
Expand Down Expand Up @@ -373,7 +372,7 @@ class DataFrame(NDFrame, OpsMixin):
convert_dates: dict[HashableT1, StataDateFormat] | None = ...,
write_index: _bool = ...,
byteorder: Literal["<", ">", "little", "big"] | None = ...,
time_stamp: _dt.datetime | None = ...,
time_stamp: dt.datetime | None = ...,
data_label: _str | None = ...,
variable_labels: dict[HashableT2, str] | None = ...,
version: Literal[114, 117, 118, 119] | None = ...,
Expand Down Expand Up @@ -1565,14 +1564,14 @@ class DataFrame(NDFrame, OpsMixin):
) -> DataFrame: ...
def at_time(
self,
time: _str | datetime.time,
time: _str | dt.time,
asof: _bool = ...,
axis: Axis | None = ...,
) -> DataFrame: ...
def between_time(
self,
start_time: _str | datetime.time,
end_time: _str | datetime.time,
start_time: _str | dt.time,
end_time: _str | dt.time,
axis: Axis | None = ...,
) -> DataFrame: ...
@overload
Expand Down Expand Up @@ -1941,7 +1940,7 @@ class DataFrame(NDFrame, OpsMixin):
level: Level | None = ...,
origin: Timestamp
| Literal["epoch", "start", "start_day", "end", "end_day"] = ...,
offset: Timedelta | _str | None = ...,
offset: dt.timedelta | _str | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

group_keys: _bool = ...,
) -> Resampler[DataFrame]: ...
def rfloordiv(
Expand All @@ -1968,7 +1967,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def rolling(
self,
window: int | str | _dt.timedelta | BaseOffset | BaseIndexer,
window: int | str | dt.timedelta | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
on: Hashable | None = ...,
Expand All @@ -1982,7 +1981,7 @@ class DataFrame(NDFrame, OpsMixin):
@overload
def rolling(
self,
window: int | str | _dt.timedelta | BaseOffset | BaseIndexer,
window: int | str | dt.timedelta | BaseOffset | BaseIndexer,
min_periods: int | None = ...,
center: _bool = ...,
on: Hashable | None = ...,
Expand Down Expand Up @@ -2217,8 +2216,8 @@ class DataFrame(NDFrame, OpsMixin):
) -> DataFrame: ...
def truncate(
self,
before: datetime.date | _str | int | None = ...,
after: datetime.date | _str | int | None = ...,
before: dt.date | _str | int | None = ...,
after: dt.date | _str | int | None = ...,
axis: Axis | None = ...,
copy: _bool = ...,
) -> DataFrame: ...
Expand Down
13 changes: 8 additions & 5 deletions pandas-stubs/core/indexes/accessors.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime as dt
from datetime import tzinfo
from datetime import (
timedelta,
tzinfo,
)
from typing import (
Generic,
Literal,
Expand Down Expand Up @@ -166,21 +169,21 @@ class _DatetimeRoundingMethods(Generic[_DTRoundingMethodReturnType]):
freq: str | BaseOffset | None,
ambiguous: Literal["raise", "infer", "NaT"] | np_ndarray_bool = ...,
nonexistent: Literal["shift_forward", "shift_backward", "NaT", "raise"]
| Timedelta = ...,
| timedelta = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

) -> _DTRoundingMethodReturnType: ...
def floor(
self,
freq: str | BaseOffset | None,
ambiguous: Literal["raise", "infer", "NaT"] | np_ndarray_bool = ...,
nonexistent: Literal["shift_forward", "shift_backward", "NaT", "raise"]
| Timedelta = ...,
| timedelta = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

) -> _DTRoundingMethodReturnType: ...
def ceil(
self,
freq: str | BaseOffset | None,
ambiguous: Literal["raise", "infer", "NaT"] | np_ndarray_bool = ...,
nonexistent: Literal["shift_forward", "shift_backward", "NaT", "raise"]
| Timedelta = ...,
| timedelta = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

) -> _DTRoundingMethodReturnType: ...

_DTNormalizeReturnType = TypeVar(
Expand All @@ -206,7 +209,7 @@ class _DatetimeLikeNoTZMethods(
tz: tzinfo | str | None,
ambiguous: Literal["raise", "infer", "NaT"] | np_ndarray_bool = ...,
nonexistent: Literal["shift_forward", "shift_backward", "NaT", "raise"]
| Timedelta = ...,
| timedelta = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

) -> _DTNormalizeReturnType: ...
def tz_convert(self, tz: tzinfo | str | None) -> _DTNormalizeReturnType: ...
def normalize(self) -> _DTNormalizeReturnType: ...
Expand Down
14 changes: 7 additions & 7 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from collections.abc import (
Sequence,
)
from datetime import (
datetime,
timedelta,
tzinfo,
)
Expand All @@ -15,7 +16,6 @@ import numpy as np
from pandas import (
DataFrame,
Index,
Timedelta,
TimedeltaIndex,
Timestamp,
)
Expand Down Expand Up @@ -59,13 +59,13 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
@overload
def __add__(self, other: TimedeltaSeries) -> TimestampSeries: ...
@overload
def __add__(self, other: Timedelta | TimedeltaIndex) -> DatetimeIndex: ...
def __add__(self, other: timedelta | TimedeltaIndex) -> DatetimeIndex: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

@overload
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
@overload
def __sub__(self, other: Timedelta | TimedeltaIndex) -> DatetimeIndex: ...
def __sub__(self, other: timedelta | TimedeltaIndex) -> DatetimeIndex: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timedelta

@overload
def __sub__(self, other: Timestamp | DatetimeIndex) -> TimedeltaIndex: ...
def __sub__(self, other: datetime | DatetimeIndex) -> TimedeltaIndex: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put back Timestamp

def to_series(self, index=..., name=...) -> TimestampSeries: ...
def snap(self, freq: str = ...): ...
def get_value(self, series, key): ...
Expand All @@ -90,7 +90,7 @@ def date_range(
start: str | DateAndDatetimeLike | None = ...,
end: str | DateAndDatetimeLike | None = ...,
periods: int | None = ...,
freq: str | timedelta | Timedelta | BaseOffset = ...,
freq: str | timedelta | BaseOffset = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: Hashable | None = ...,
Expand All @@ -102,7 +102,7 @@ def bdate_range(
start: str | DateAndDatetimeLike | None = ...,
end: str | DateAndDatetimeLike | None = ...,
periods: int | None = ...,
freq: str | timedelta | Timedelta | BaseOffset = ...,
freq: str | timedelta | BaseOffset = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: Hashable | None = ...,
Expand All @@ -116,7 +116,7 @@ def bdate_range(
end: str | DateAndDatetimeLike | None = ...,
periods: int | None = ...,
*,
freq: str | timedelta | Timedelta | BaseOffset,
freq: str | timedelta | BaseOffset,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: Hashable | None = ...,
Expand Down
Loading