Skip to content

Add timedelta as possible freq parameter type to date_range() and bdate_range() #322

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 2 commits into from
Sep 22, 2022
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
9 changes: 6 additions & 3 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import tzinfo
from datetime import (
timedelta,
tzinfo,
)
from typing import overload

import numpy as np
Expand Down Expand Up @@ -90,7 +93,7 @@ def date_range(
start: str | DatetimeLike | None = ...,
end: str | DatetimeLike | None = ...,
periods: int | None = ...,
freq: str | BaseOffset = ...,
freq: str | timedelta | BaseOffset = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also add pd.Timedelta as a valid argument here? It works as well (and for bdate_range)

tz: str | tzinfo = ...,
normalize: bool = ...,
name: str | None = ...,
Expand All @@ -101,7 +104,7 @@ def bdate_range(
start: str | DatetimeLike | None = ...,
end: str | DatetimeLike | None = ...,
periods: int | None = ...,
freq: str | BaseOffset = ...,
freq: str | timedelta | BaseOffset = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: str | None = ...,
Expand Down
16 changes: 15 additions & 1 deletion tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ def test_timedeltaindex_accessors() -> None:

def test_some_offsets() -> None:
# GH 222

check(
assert_type(
CustomBusinessDay(calendar=USFederalHolidayCalendar()), CustomBusinessDay
Expand All @@ -484,6 +483,21 @@ def test_some_offsets() -> None:
)
# GH 224
check(assert_type(dt.date.today() - Day(), dt.date), dt.date)
# GH 235
check(
assert_type(
pd.date_range("1/1/2022", "2/1/2022", freq=dt.timedelta(days=2)),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)
check(
assert_type(
pd.bdate_range("1/1/2022", "2/1/2022", freq=dt.timedelta(days=2)),
pd.DatetimeIndex,
),
pd.DatetimeIndex,
)


def test_types_to_numpy() -> None:
Expand Down