TYP: Generic timedelta64
and datetime64
scalar types
#27790
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This makes the
np.timedelta64
andnp.datetime64
scalar types generic with one type parameter, matching the return type of their.item()
(and.tolist()
) methods.The type parameters are bound to
datetime.timedelta | int | None
anddatetime.date | datetime.datetime | int | None
fortimedelta64
anddatetime64
, respectively.Both type-parameters are optional for backwards compatibility, and relate to the time-units in the following way:
+T@timedelta64
+T@datetime64
NaT
None
None
Y
int
date
M
int
date
W
timedelta
date
D
timedelta
date
h
timedelta
datetime
m
timedelta
datetime
s
timedelta
datetime
ms
timedelta
datetime
us
timedelta
datetime
ns
int
int
ps
int
int
fs
int
int
as
int
int
Static type checkers are aware of these units, and will infer e.g.
timedelta64(1, "D")
astimedelta64[timedelta]
,timedelta64("NaT")
astimedelta64[None]
, anddatetime64(b"today")
asdatetime64[date]
.The (scalar) arithmetic operators have been made aware of the type-parameters. So
NaT
will always propagate when adding, subtracting, etc, and the addition ofdatetime64[date]
andtimedelta64[int]
will be inferred asdatetime64[int]
.Note that this only applies to the scalar operators. So the
ndarray
operators, for instance, do not yet support these generictimedelta64
anddatetime64
type parameters, and will continue to behave as if these type parameters didn't exist.