Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/29396.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Show unit information in ``__repr__`` for ``datetime64("NaT")``
------------------------------------------------------------------
When a `datetime64` object is "Not a Time" (NaT), its ``__repr__`` method now
includes the time unit of the datetime64 type. This makes it consistent with
the behavior of a `timedelta64` object.
21 changes: 16 additions & 5 deletions numpy/_core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,23 @@ datetimetype_repr(PyObject *self)
if (legacy_print_mode == -1) {
return NULL;
}
if (legacy_print_mode > 125) {
ret = PyUnicode_FromFormat("np.datetime64('%s')", iso);
}
else {
ret = PyUnicode_FromFormat("numpy.datetime64('%s')", iso);

PyObject *meta = metastr_to_unicode(&scal->obmeta, 1);
if((scal->obval == NPY_DATETIME_NAT) && (meta != NULL)){
if (legacy_print_mode > 125) {
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.

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
Copy Markdown
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.

ret = PyUnicode_FromFormat("np.datetime64('%s','%S')", iso, meta);
} else {
ret = PyUnicode_FromFormat("numpy.datetime64('%s','%S')", iso, meta);
}
} else {
if (legacy_print_mode > 125) {
ret = PyUnicode_FromFormat("np.datetime64('%s')", iso);
}
else {
ret = PyUnicode_FromFormat("numpy.datetime64('%s')", iso);
}
}

}
else {
PyObject *meta = metastr_to_unicode(&scal->obmeta, 1);
Expand Down
4 changes: 3 additions & 1 deletion numpy/_core/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,12 @@ def test_datetime_scalar_construction(self):
# Some basic strings and repr
assert_equal(str(np.datetime64('NaT')), 'NaT')
assert_equal(repr(np.datetime64('NaT')),
"np.datetime64('NaT')")
"np.datetime64('NaT','generic')")
assert_equal(str(np.datetime64('2011-02')), '2011-02')
assert_equal(repr(np.datetime64('2011-02')),
"np.datetime64('2011-02')")
assert_equal(repr(np.datetime64('NaT').astype(np.dtype("datetime64[ns]"))),
"np.datetime64('NaT','ns')")

# None gets constructed as NaT
assert_equal(np.datetime64(None), np.datetime64('NaT'))
Expand Down
Loading