Skip to content

fix various issues with offsets #235

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 3 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions pandas-stubs/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import (
date,
datetime,
timedelta,
)
Expand All @@ -15,10 +16,12 @@ from pandas.core.indexes.datetimes import DatetimeIndex

from pandas._typing import npt

from pandas.tseries.holiday import AbstractHolidayCalendar

from .timedeltas import Timedelta

_BaseOffsetT = TypeVar("_BaseOffsetT", bound=BaseOffset)
_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
_DatetimeT = TypeVar("_DatetimeT", bound=date)
_TimedeltaT = TypeVar("_TimedeltaT", bound=timedelta)

prefix_mapping: dict[str, type]
Expand Down Expand Up @@ -204,8 +207,8 @@ class CustomBusinessDay(BusinessDay):
self,
n: int = ...,
normalize: bool = ...,
offset: timedelta = ...,
weekmask: str = ...,
holidays: list = ...,
calendar: AbstractHolidayCalendar | np.busdaycalendar = ...,
): ...

class CustomBusinessHour(BusinessHour):
Expand Down
2 changes: 2 additions & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ PandasScalar = Union[bytes, datetime.date, datetime.datetime, datetime.timedelta
# Scalar = Union[PythonScalar, PandasScalar]
IntStrT = TypeVar("IntStrT", int, str)

DatetimeLike = Union[datetime.date, datetime.datetime, np.datetime64, Timestamp]

# dtypes
NpDtype = Union[str, np.dtype[np.generic], type[Union[str, complex, bool, object]]]
Dtype = Union[ExtensionDtype, NpDtype]
Expand Down
42 changes: 25 additions & 17 deletions pandas-stubs/core/indexes/datetimes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ from pandas.core.series import (
from pandas._typing import (
AnyArrayLike,
ArrayLike,
DatetimeLike,
IntervalClosedType,
np_ndarray_bool,
)

from pandas.core.dtypes.dtypes import DatetimeTZDtype

from pandas.tseries.offsets import (
BaseOffset,
Tick,
)

class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeIndexProperties):
tz: tzinfo | None
def __init__(
Expand Down Expand Up @@ -76,25 +83,26 @@ class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeIndexProperties):
def dtype(self) -> np.dtype | DatetimeTZDtype: ...

def date_range(
start=...,
end=...,
periods=...,
freq=...,
tz=...,
normalize=...,
name=...,
closed=...,
start: str | DatetimeLike | None = ...,
end: str | DatetimeLike | None = ...,
periods: int | None = ...,
freq: str | BaseOffset | Tick = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name: str | None = ...,
inclusive: IntervalClosedType = ...,
**kwargs,
) -> DatetimeIndex: ...
def bdate_range(
start=...,
end=...,
periods=...,
freq: str = ...,
tz=...,
start: str | DatetimeLike | None = ...,
end: str | DatetimeLike | None = ...,
periods: int | None = ...,
freq: str | BaseOffset | Tick = ...,
tz: str | tzinfo = ...,
normalize: bool = ...,
name=...,
weekmask=...,
holidays=...,
closed=...,
name: str | None = ...,
weekmask: str | None = ...,
holidays: list | None = ...,
inclusive: IntervalClosedType = ...,
**kwargs,
) -> DatetimeIndex: ...
3 changes: 3 additions & 0 deletions pandas-stubs/tseries/holiday.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Holiday: ...
class AbstractHolidayCalendar: ...
class USFederalHolidayCalendar(AbstractHolidayCalendar): ...
37 changes: 37 additions & 0 deletions tests/test_timefuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
check,
)

from pandas.tseries.holiday import USFederalHolidayCalendar
from pandas.tseries.offsets import (
BusinessDay,
CustomBusinessDay,
Day,
)

if TYPE_CHECKING:
from pandas.core.series import (
PeriodSeries,
Expand Down Expand Up @@ -403,3 +410,33 @@ def test_datetimeindex_accessors() -> None:
check(assert_type(i0.month_name(), pd.Index), pd.Index, str)
check(assert_type(i0.day_name(), pd.Index), pd.Index, str)
check(assert_type(i0.is_normalized, bool), bool)


def test_some_offsets() -> None:
# GH 222

check(
assert_type(
CustomBusinessDay(calendar=USFederalHolidayCalendar()), CustomBusinessDay
),
CustomBusinessDay,
)
# GH 223
check(
assert_type(pd.date_range("1/1/2022", "2/1/2022", freq="1D"), pd.DatetimeIndex),
pd.DatetimeIndex,
)
check(
assert_type(
pd.date_range("1/1/2022", "2/1/2022", freq=Day()), pd.DatetimeIndex
),
pd.DatetimeIndex,
)
check(
assert_type(
pd.bdate_range("1/1/2022", "2/1/2022", freq=BusinessDay()), pd.DatetimeIndex
),
pd.DatetimeIndex,
)
# GH 224
check(assert_type(dt.date.today() - Day(), dt.date), dt.date)