Skip to content

Conversation

@riku-sakamoto
Copy link
Contributor

Overview

Details

Unlike timedelta objects, datetime64 does not display its unit in the repr output (see numpy#28496).
To maintain backward compatibility, this change adds unit information only when the datetime64 object has a NaT value.
@riku-sakamoto riku-sakamoto marked this pull request as ready for review July 19, 2025 08:53
Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Makes sense to me. I think because this changes the repr it needs a release note, see doc/release/upcoming_changes/README.rst for more info about adding release note blurbs to PRs.


PyObject *meta = metastr_to_unicode(&scal->obmeta, 1);
if((scal->obval == NPY_DATETIME_NAT) && (meta != NULL)){
if (legacy_print_mode > 125) {
Copy link
Member

Choose a reason for hiding this comment

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

I guess technically we could define a new numpy 2.3 legacy print mode as well. I don't know if it's worth the ceremony just for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comment! Defining a new print mode feels like a bit too much as well, but I'm happy to give it a try if needed.

…aT")

Add a new release note for the change that shows time unit in the __repr__
output of datetime64("NaT"). See numpygh-29396.
@riku-sakamoto
Copy link
Contributor Author

riku-sakamoto commented Jul 21, 2025

Makes sense to me. I think because this changes the repr it needs a release note, see doc/release/upcoming_changes/README.rst for more info about adding release note blurbs to PRs.

@ngoldbaum
Thank you for the review! I've added a release note as suggested.

@ngoldbaum
Copy link
Member

Let's bring it in. If anyone reads this in the future because of downstream test failures please open an issue and we can reconsider adding a new legacy print mode.

@ngoldbaum ngoldbaum merged commit d2c0c10 into numpy:main Jul 21, 2025
76 checks passed
@ngoldbaum
Copy link
Member

Thanks @riku-sakamoto

IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
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.

BUG: np.datetime64("nat", "ms") does not use unit in repr (unlike timedelta)

2 participants