Skip to content

BUG: raise warning when np.timedelta64() is called - #31213

Merged
seberg merged 7 commits into
numpy:mainfrom
riku-sakamoto:raise_warning_for_default_constructor_of_timedelta
Apr 20, 2026
Merged

seberg merged 7 commits into
numpy:mainfrom
riku-sakamoto:raise_warning_for_default_constructor_of_timedelta

Conversation

@riku-sakamoto

@riku-sakamoto riku-sakamoto commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

PR summary

Closes #31103.

  • Raise a DeprecationWarning when following codes are called without specific unit
    • np.timedelta()
    • np.datetime()
    • np.datetime("NaT")

AI Disclosure

No AI tools are used.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let's hope that this doesn't cause pain due to the default construction, but I'll guess it shouldn't be too bad.

Comment thread numpy/_core/tests/test_deprecations.py Resolved
@seberg seberg added this to the 2.5.0 Release milestone Apr 17, 2026
@riku-sakamoto

Copy link
Copy Markdown
Contributor Author

@seberg

Thank you for the review!
Would you prefer to include changes about raising a warning about np.datetime64() ?
If so, I'll add it and update the PR. (maybe need a release note as well?)

@seberg

seberg commented Apr 17, 2026

Copy link
Copy Markdown
Member

Ah, thanks, sorry I missed that you didn't warn it because it was NaT and it is a bit less important. Since I think we warn almost everywhere else, maybe we might as well try it?

I would think the old release note sufficiently captures things, but either way is fine.

@riku-sakamoto

Copy link
Copy Markdown
Contributor Author

@seberg
Thank you as always!
I've updated to raise a warning when np.datetime64() or np.timedelta64("NaT") is called with no unit.

@seberg seberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @riku-sakamoto, let's try this.

If this makes the deprecation worse for downstream, at least around NaT scalar creation where generic units make a bit more sense let us know.
(But since we didn't make an exception for 0 timedelta right now either, I think, this does seem reasonable to me.)

@seberg
seberg merged commit 49d0be4 into numpy:main Apr 20, 2026
86 checks passed
@lucascolley

Copy link
Copy Markdown
Contributor

@seberg I think the example of np.datetime64('nat') at https://numpy.org/devdocs/reference/arrays.datetime.html#basic-datetimes needs to be updated too.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: np.timedelta64() and np.datetime64() do not raise any warning

4 participants