Skip to content

Commit 07f9398

Browse files
authored
fix various issues with offsets (#235)
* fix various issues with offsets * Add holidays.pyi * remove Tick as it is subclass of BaseOffset
1 parent fc3a5f3 commit 07f9398

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

pandas-stubs/_libs/tslibs/offsets.pyi

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import (
2+
date,
23
datetime,
34
timedelta,
45
)
@@ -15,10 +16,12 @@ from pandas.core.indexes.datetimes import DatetimeIndex
1516

1617
from pandas._typing import npt
1718

19+
from pandas.tseries.holiday import AbstractHolidayCalendar
20+
1821
from .timedeltas import Timedelta
1922

2023
_BaseOffsetT = TypeVar("_BaseOffsetT", bound=BaseOffset)
21-
_DatetimeT = TypeVar("_DatetimeT", bound=datetime)
24+
_DatetimeT = TypeVar("_DatetimeT", bound=date)
2225
_TimedeltaT = TypeVar("_TimedeltaT", bound=timedelta)
2326

2427
prefix_mapping: dict[str, type]
@@ -204,8 +207,8 @@ class CustomBusinessDay(BusinessDay):
204207
self,
205208
n: int = ...,
206209
normalize: bool = ...,
207-
offset: timedelta = ...,
208-
weekmask: str = ...,
210+
holidays: list = ...,
211+
calendar: AbstractHolidayCalendar | np.busdaycalendar = ...,
209212
): ...
210213

211214
class CustomBusinessHour(BusinessHour):

pandas-stubs/_typing.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ PandasScalar = Union[bytes, datetime.date, datetime.datetime, datetime.timedelta
4747
# Scalar = Union[PythonScalar, PandasScalar]
4848
IntStrT = TypeVar("IntStrT", int, str)
4949

50+
DatetimeLike = Union[datetime.date, datetime.datetime, np.datetime64, Timestamp]
51+
5052
# dtypes
5153
NpDtype = Union[str, np.dtype[np.generic], type[Union[str, complex, bool, object]]]
5254
Dtype = Union[ExtensionDtype, NpDtype]

pandas-stubs/core/indexes/datetimes.pyi

+22-17
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ from pandas.core.series import (
1919
from pandas._typing import (
2020
AnyArrayLike,
2121
ArrayLike,
22+
DatetimeLike,
23+
IntervalClosedType,
2224
np_ndarray_bool,
2325
)
2426

2527
from pandas.core.dtypes.dtypes import DatetimeTZDtype
2628

29+
from pandas.tseries.offsets import BaseOffset
30+
2731
class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeIndexProperties):
2832
tz: tzinfo | None
2933
def __init__(
@@ -76,25 +80,26 @@ class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeIndexProperties):
7680
def dtype(self) -> np.dtype | DatetimeTZDtype: ...
7781

7882
def date_range(
79-
start=...,
80-
end=...,
81-
periods=...,
82-
freq=...,
83-
tz=...,
84-
normalize=...,
85-
name=...,
86-
closed=...,
83+
start: str | DatetimeLike | None = ...,
84+
end: str | DatetimeLike | None = ...,
85+
periods: int | None = ...,
86+
freq: str | BaseOffset = ...,
87+
tz: str | tzinfo = ...,
88+
normalize: bool = ...,
89+
name: str | None = ...,
90+
inclusive: IntervalClosedType = ...,
8791
**kwargs,
8892
) -> DatetimeIndex: ...
8993
def bdate_range(
90-
start=...,
91-
end=...,
92-
periods=...,
93-
freq: str = ...,
94-
tz=...,
94+
start: str | DatetimeLike | None = ...,
95+
end: str | DatetimeLike | None = ...,
96+
periods: int | None = ...,
97+
freq: str | BaseOffset = ...,
98+
tz: str | tzinfo = ...,
9599
normalize: bool = ...,
96-
name=...,
97-
weekmask=...,
98-
holidays=...,
99-
closed=...,
100+
name: str | None = ...,
101+
weekmask: str | None = ...,
102+
holidays: list | None = ...,
103+
inclusive: IntervalClosedType = ...,
104+
**kwargs,
100105
) -> DatetimeIndex: ...

pandas-stubs/tseries/holiday.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Holiday: ...
2+
class AbstractHolidayCalendar: ...
3+
class USFederalHolidayCalendar(AbstractHolidayCalendar): ...

tests/test_timefuncs.py

+37
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
check,
2424
)
2525

26+
from pandas.tseries.holiday import USFederalHolidayCalendar
27+
from pandas.tseries.offsets import (
28+
BusinessDay,
29+
CustomBusinessDay,
30+
Day,
31+
)
32+
2633
if TYPE_CHECKING:
2734
from pandas.core.series import (
2835
PeriodSeries,
@@ -403,3 +410,33 @@ def test_datetimeindex_accessors() -> None:
403410
check(assert_type(i0.month_name(), pd.Index), pd.Index, str)
404411
check(assert_type(i0.day_name(), pd.Index), pd.Index, str)
405412
check(assert_type(i0.is_normalized, bool), bool)
413+
414+
415+
def test_some_offsets() -> None:
416+
# GH 222
417+
418+
check(
419+
assert_type(
420+
CustomBusinessDay(calendar=USFederalHolidayCalendar()), CustomBusinessDay
421+
),
422+
CustomBusinessDay,
423+
)
424+
# GH 223
425+
check(
426+
assert_type(pd.date_range("1/1/2022", "2/1/2022", freq="1D"), pd.DatetimeIndex),
427+
pd.DatetimeIndex,
428+
)
429+
check(
430+
assert_type(
431+
pd.date_range("1/1/2022", "2/1/2022", freq=Day()), pd.DatetimeIndex
432+
),
433+
pd.DatetimeIndex,
434+
)
435+
check(
436+
assert_type(
437+
pd.bdate_range("1/1/2022", "2/1/2022", freq=BusinessDay()), pd.DatetimeIndex
438+
),
439+
pd.DatetimeIndex,
440+
)
441+
# GH 224
442+
check(assert_type(dt.date.today() - Day(), dt.date), dt.date)

0 commit comments

Comments
 (0)