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 3 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
2 changes: 1 addition & 1 deletion pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class Timedelta(timedelta):
@overload
def __rsub__(self, other: timedelta | Timedelta | np.timedelta64) -> Timedelta: ...
@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
13 changes: 2 additions & 11 deletions pandas-stubs/_libs/tslibs/timestamps.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ from datetime import (
timedelta,
tzinfo as _tzinfo,
)
import sys
from time import struct_time
from typing import (
ClassVar,
Expand Down Expand Up @@ -100,16 +99,8 @@ class Timestamp(datetime):
def tz(self) -> _tzinfo | None: ...
@property
def fold(self) -> int: ...

if sys.version_info < (3, 12):
@classmethod
def fromtimestamp(cls, t: float, tz: _tzinfo | str | None = ...) -> Self: ...
else:
@classmethod
def fromtimestamp( # pyright: ignore[reportIncompatibleMethodOverride]
cls, t: float, tz: _tzinfo | str | None = ...
) -> Self: ...

@classmethod
def fromtimestamp(cls, t: float, tz: _tzinfo | str | None = ...) -> Self: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need to rebase against main

@classmethod
def utcfromtimestamp(cls, ts: float) -> Self: ...
@classmethod
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
7 changes: 4 additions & 3 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 Down Expand Up @@ -59,13 +60,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 Down
11 changes: 4 additions & 7 deletions pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ from pandas.core.indexes.period import PeriodIndex
from pandas.core.series import TimedeltaSeries
from typing_extensions import Self

from pandas._libs import (
Timedelta,
Timestamp,
)
from pandas._libs import Timedelta
from pandas._libs.tslibs import BaseOffset
from pandas._typing import (
AnyArrayLike,
Expand Down Expand Up @@ -52,9 +49,9 @@ class TimedeltaIndex(DatetimeTimedeltaMixin[Timedelta], TimedeltaIndexProperties
@overload
def __add__(self, other: DatetimeIndex) -> DatetimeIndex: ...
@overload
def __add__(self, other: Timedelta | Self) -> Self: ...
def __radd__(self, other: Timestamp | DatetimeIndex) -> DatetimeIndex: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __sub__(self, other: Timedelta | Self) -> Self: ...
def __add__(self, other: dt.timedelta | Self) -> Self: ...
def __radd__(self, other: dt.datetime | DatetimeIndex) -> DatetimeIndex: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __sub__(self, other: dt.timedelta | Self) -> Self: ...
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 and Timestamp

def __mul__(self, other: num) -> Self: ...
@overload # type: ignore[override]
def __truediv__(self, other: num | Sequence[float]) -> Self: ...
Expand Down
4 changes: 2 additions & 2 deletions pandas-stubs/core/reshape/merge.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import timedelta
from typing import (
Literal,
overload,
Expand All @@ -8,7 +9,6 @@ from pandas import (
Series,
)

from pandas._libs.tslibs import Timedelta
from pandas._typing import (
AnyArrayLike,
HashableT,
Expand Down Expand Up @@ -99,7 +99,7 @@ def merge_asof(
| tuple[str, str]
| tuple[None, str]
| tuple[str, None] = ...,
tolerance: int | Timedelta | None = ...,
tolerance: int | timedelta | 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

allow_exact_matches: bool = ...,
direction: Literal["backward", "forward", "nearest"] = ...,
) -> DataFrame: ...
16 changes: 8 additions & 8 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1352,9 +1352,9 @@ class Series(IndexOpsMixin[S1], NDFrame):
base: int = ...,
on: _str | None = ...,
level: Level | None = ...,
origin: Timestamp
origin: datetime
| Literal["epoch", "start", "start_day", "end", "end_day"] = ...,
offset: Timedelta | _str | None = ...,
offset: 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

) -> Resampler[Series]: ...
def first(self, offset) -> Series[S1]: ...
def last(self, offset) -> Series[S1]: ...
Expand Down Expand Up @@ -1456,7 +1456,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __add__(self, other: S1 | Self) -> Self: ...
@overload
def __add__(
self, other: num | _str | Timedelta | _ListLike | Series | np.timedelta64
self, other: num | _str | timedelta | _ListLike | Series | np.timedelta64
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

) -> Series: ...
# ignore needed for mypy as we want different results based on the arguments
@overload # type: ignore[override]
Expand Down Expand Up @@ -1485,7 +1485,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
) -> Series[_bool]: ...
@overload
def __mul__(
self, other: Timedelta | TimedeltaSeries | np.timedelta64
self, other: timedelta | TimedeltaSeries | np.timedelta64
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

) -> TimedeltaSeries: ...
@overload
def __mul__(self, other: num | _ListLike | Series) -> Series: ...
Expand Down Expand Up @@ -2043,18 +2043,18 @@ class TimedeltaSeries(Series[Timedelta]):
def __add__(self, other: Period) -> PeriodSeries: ...
@overload
def __add__(
self, other: Timestamp | TimestampSeries | DatetimeIndex
self, other: datetime | TimestampSeries | 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 Timestamp

) -> TimestampSeries: ...
@overload
def __add__( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: Timedelta | np.timedelta64
self, other: timedelta | np.timedelta64
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

) -> TimedeltaSeries: ...
def __radd__(self, other: Timestamp | TimestampSeries) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
def __radd__(self, other: datetime | TimestampSeries) -> TimestampSeries: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
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 __mul__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: num | Sequence[num] | Series[int] | Series[float]
) -> TimedeltaSeries: ...
def __sub__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
self, other: Timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64
self, other: timedelta | TimedeltaSeries | TimedeltaIndex | np.timedelta64
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

) -> TimedeltaSeries: ...
@overload # type: ignore[override]
def __truediv__(self, other: float | Sequence[float]) -> Self: ...
Expand Down
6 changes: 3 additions & 3 deletions pandas-stubs/tseries/holiday.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def register(cls: type[AbstractHolidayCalendar]) -> None: ...
def get_calendar(name: str) -> AbstractHolidayCalendar: ...

class AbstractHolidayCalendar:
rules: list[Holiday] = ...
start_date: Timestamp = ...
end_date: Timestamp = ...
rules: list[Holiday]
start_date: Timestamp
end_date: Timestamp

def __init__(self, name: str = "", rules: list[Holiday] | None = None) -> None: ...
def rule_from_name(self, name: str) -> Holiday | None: ...
Expand Down
11 changes: 11 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,3 +1234,14 @@ def test_date_range_unit():
),
pd.DatetimeIndex,
)


def test_DatetimeIndex_sub_timedelta() -> None:
# GH838
check(
assert_type(
pd.date_range("2023-01-01", periods=10, freq="1D") - dt.timedelta(days=1),
"pd.DatetimeIndex",
),
pd.DatetimeIndex,
)