Skip to content
Merged
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
43 changes: 39 additions & 4 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,46 @@ class DatetimeIndex(
self, periods: int = 1, freq: DateOffset | Timedelta | str | None = None
) -> Self: ...

@overload
def date_range(
start: str | DateAndDatetimeLike,
end: str | DateAndDatetimeLike,
freq: str | timedelta | Timedelta | BaseOffset | None = None,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
inclusive: IntervalClosedType = "both",
unit: TimeUnit | None = None,
) -> DatetimeIndex: ...
@overload
def date_range(
start: str | DateAndDatetimeLike | None = None,
end: str | DateAndDatetimeLike | None = None,
periods: int | None = None,
freq: str | timedelta | Timedelta | BaseOffset = "D",
start: str | DateAndDatetimeLike,
end: str | DateAndDatetimeLike,
periods: int,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
inclusive: IntervalClosedType = "both",
unit: TimeUnit | None = None,
) -> DatetimeIndex: ...
@overload
def date_range(
start: str | DateAndDatetimeLike,
*,
periods: int,
freq: str | timedelta | Timedelta | BaseOffset | None = None,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
inclusive: IntervalClosedType = "both",
unit: TimeUnit | None = None,
) -> DatetimeIndex: ...
@overload
def date_range(
*,
end: str | DateAndDatetimeLike,
periods: int,
freq: str | timedelta | Timedelta | BaseOffset | None = None,
tz: TimeZones = None,
normalize: bool = False,
name: Hashable | None = None,
Expand Down
36 changes: 33 additions & 3 deletions pandas-stubs/core/indexes/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,43 @@ class TimedeltaIndex(
def to_series(self, index=..., name: Hashable = ...) -> TimedeltaSeries: ...
def shift(self, periods: int = 1, freq=...) -> Self: ...

@overload
def timedelta_range(
start: TimedeltaConvertibleTypes | None = None,
end: TimedeltaConvertibleTypes | None = None,
periods: int | None = None,
start: TimedeltaConvertibleTypes,
end: TimedeltaConvertibleTypes,
*,
freq: str | DateOffset | Timedelta | dt.timedelta | None = None,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
) -> TimedeltaIndex: ...
@overload
def timedelta_range(
*,
end: TimedeltaConvertibleTypes,
periods: int,
freq: str | DateOffset | Timedelta | dt.timedelta | None = None,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
) -> TimedeltaIndex: ...
@overload
def timedelta_range(
start: TimedeltaConvertibleTypes,
*,
periods: int,
freq: str | DateOffset | Timedelta | dt.timedelta | None = None,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
) -> TimedeltaIndex: ...
@overload
def timedelta_range(
start: TimedeltaConvertibleTypes,
end: TimedeltaConvertibleTypes,
periods: int,
*,
name: Hashable | None = None,
closed: Literal["left", "right"] | None = None,
unit: None | str = ...,
) -> TimedeltaIndex: ...
5 changes: 4 additions & 1 deletion tests/test_styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pandas._typing import Scalar

from tests import (
PD_LTE_23,
check,
ensure_clean,
)
Expand Down Expand Up @@ -141,7 +142,9 @@ def test_highlight_quantile() -> None:


def test_loader() -> None:
check(assert_type(DF.style.loader, PackageLoader), PackageLoader)
if PD_LTE_23:
# see GH62123 for pandas main repo, type changes in pandas 3.0
check(assert_type(DF.style.loader, PackageLoader), PackageLoader)


def test_pipe() -> None:
Expand Down
104 changes: 104 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,110 @@ def test_date_range_unit():
)


def test_date_range_overloads() -> None:
"""Test different overloads of pd.date_range (GH1327)."""
t1 = pd.Timestamp("2023-04-05")
t2 = pd.Timestamp("2023-05-05")
# start end (freq None)
check(assert_type(pd.date_range(t1, t2), pd.DatetimeIndex), pd.DatetimeIndex)
# start end positional (freq None)
check(
assert_type(pd.date_range(start=t1, end=t2), pd.DatetimeIndex), pd.DatetimeIndex
)
# start periods (freq None)
check(
assert_type(pd.date_range(start=t1, periods=10), pd.DatetimeIndex),
pd.DatetimeIndex,
)
# end periods (freq None)
check(
assert_type(pd.date_range(end=t2, periods=10), pd.DatetimeIndex),
pd.DatetimeIndex,
)
# start periods (freq None)
check(assert_type(pd.date_range(t1, t2, 10), pd.DatetimeIndex), pd.DatetimeIndex)
# start end periods
check(
assert_type(pd.date_range(start=t1, end=t2, periods=10), pd.DatetimeIndex),
pd.DatetimeIndex,
)
# start end freq
check(
assert_type(pd.date_range(start=t1, end=t2, freq="ME"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
# start periods freq
check(
assert_type(pd.date_range(start=t1, periods=10, freq="ME"), pd.DatetimeIndex),
pd.DatetimeIndex,
)

if TYPE_CHECKING_INVALID_USAGE:
pd.date_range(t1) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.date_range(start=t1) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.date_range(end=t1) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.date_range(periods=10) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.date_range(freq="BD") # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.date_range(start=t1, end=t2, periods=10, freq="BD") # type: ignore[call-overload] # pyright: ignore[reportCallIssue]


def test_timedelta_range_overloads() -> None:
"""Test different overloads of pd.timedelta_range (GH1327)."""
t1 = "1 day"
t2 = "20 day"
# start end (freq None)
check(assert_type(pd.timedelta_range(t1, t2), pd.TimedeltaIndex), pd.TimedeltaIndex)
# start end positional (freq None)
check(
assert_type(pd.timedelta_range(start=t1, end=t2), pd.TimedeltaIndex),
pd.TimedeltaIndex,
)
# start periods (freq None)
check(
assert_type(pd.timedelta_range(start=t1, periods=10), pd.TimedeltaIndex),
pd.TimedeltaIndex,
)
# end periods (freq None)
check(
assert_type(pd.timedelta_range(end=t2, periods=10), pd.TimedeltaIndex),
pd.TimedeltaIndex,
)
# start periods (freq None)
check(
assert_type(pd.timedelta_range(t1, t2, 10), pd.TimedeltaIndex),
pd.TimedeltaIndex,
)
# start end periods
check(
assert_type(
pd.timedelta_range(start=t1, end=t2, periods=10), pd.TimedeltaIndex
),
pd.TimedeltaIndex,
)
# start end freq
check(
assert_type(
pd.timedelta_range(start=t1, end=t2, freq="48h"), pd.TimedeltaIndex
),
pd.TimedeltaIndex,
)
# start periods freq
check(
assert_type(
pd.timedelta_range(start=t1, periods=10, freq="48h"), pd.TimedeltaIndex
),
pd.TimedeltaIndex,
)

if TYPE_CHECKING_INVALID_USAGE:
pd.timedelta_range(t1) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.timedelta_range(start=t1) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.timedelta_range(end=t1) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.timedelta_range(periods=10) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.timedelta_range(freq="BD") # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
pd.timedelta_range(start=t1, end=t2, periods=10, freq="BD") # type: ignore[call-overload] # pyright: ignore[reportCallIssue]


def test_DatetimeIndex_sub_timedelta() -> None:
# GH838
check(
Expand Down
Loading