Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

TYP: Generic timedelta64 and datetime64 scalar types #27790

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 1 commit into from
Nov 18, 2024

Conversation

jorenham
Copy link
Member

This makes the np.timedelta64 and np.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 and datetime.date | datetime.datetime | int | None for timedelta64 and datetime64, 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") as timedelta64[timedelta], timedelta64("NaT") as timedelta64[None], and datetime64(b"today") as datetime64[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 of datetime64[date] and timedelta64[int] will be inferred as datetime64[int].

Note that this only applies to the scalar operators. So the ndarray operators, for instance, do not yet support these generic timedelta64 and datetime64 type parameters, and will continue to behave as if these type parameters didn't exist.

@charris charris merged commit 484f9bf into numpy:main Nov 18, 2024
70 checks passed
@charris
Copy link
Member

charris commented Nov 18, 2024

Thanks Joren.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.