TYP: Allow time manipulation functions to accept date and timedelta objects#20750
TYP: Allow time manipulation functions to accept date and timedelta objects#20750charris merged 1 commit intonumpy:mainfrom
date and timedelta objects#20750Conversation
| cls, | ||
| weekmask: ArrayLike = ..., | ||
| holidays: ArrayLike = ..., | ||
| holidays: ArrayLike | dt.date | _NestedSequence[dt.date] = ..., |
There was a problem hiding this comment.
why not create a type alias for ArrayLike | dt.date | _NestedSequence[dt.date] ?
There was a problem hiding this comment.
Considering they're only applicable for very limited (and niche) set of functions, I'm not sure it's worthwhile to introduce aliases here. Line length is also not an issue here.
| def busday_count( # type: ignore[misc] | ||
| begindates: _ScalarLike_co, | ||
| enddates: _ScalarLike_co, | ||
| begindates: _ScalarLike_co | dt.date, |
There was a problem hiding this comment.
ditto for a type alias for: _ScalarLike_co | dt.date
| def busday_offset( # type: ignore[misc] | ||
| dates: ArrayLike, | ||
| offsets: ArrayLike, | ||
| dates: ArrayLike | dt.date | _NestedSequence[dt.date], |
There was a problem hiding this comment.
should dates and offsets be of the same type here? That might be overkill but something like:
NTD = TypeVar("NTD", bound=ArrayLike | dt.date | _NestedSequence[dt.date])
....
dates: NTD
offsets: NTDThere was a problem hiding this comment.
|
Thanks Bas. |
|
Hmm, this doesn't backport cleanly. Are there other backports that should be made? |
|
I suspect just keeping |
data and timedelta objectsdate and timedelta objects
Correct, just in case I just created a backport PR at #20763
There is a small fix for #20756 that I'll make in a bit, but that's about it for now as far as typing-based backport candidates are concerned. |
Closes #20746
The time manipulation functions (e.g.
busday_count) are somewhat unique in that they unsafely cast many of their inputs todatetime64ortimedelta64arrays. This unsafe casting means that the conventionally usednpt.ArrayLikealias is not comprehensive enough to capture all valid input types, most notably excludingdatetime.dateanddatetime.datetime.This PR thus expands the relevant argument types with support for more
datetime-based objects.