8000 Merge pull request #17645 from lormuc/timedelta-nat-format · numpy/numpy@4e61c89 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e61c89

Browse files
authored
Merge pull request #17645 from lormuc/timedelta-nat-format
BUG: fix np.timedelta64('nat').__format__ throwing an exception
2 parents 3dca0c7 + db6661c commit 4e61c89

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ gentype_format(PyObject *self, PyObject *args)
274274
if (Py_TYPE(self) == &PyBoolArrType_Type) {
275275
obj = PyBool_FromLong(PyArrayScalar_VAL(self, Bool));
276276
}
277-
else if (PyArray_IsScalar(self, Integer)) {
277+
else if (PyArray_IsScalar(self, Integer)
278+
&& !PyArray_IsScalar(self, Timedelta)) {
278279
obj = Py_TYPE(self)->tp_as_number->nb_int(self);
279280
}
280281
else if (PyArray_IsScalar(self, Floating)) {

numpy/core/tests/test_datetime.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ def test_timedelta_0_dim_object_array_conversion(self):
430430
np.timedelta64)
431431
assert_equal(actual, expected)
432432

433+
def test_timedelta_nat_format(self):
434+
# gh-17552
435+
assert_equal('NaT', '{0}'.format(np.timedelta64('nat')))
436+
433437
def test_timedelta_scalar_construction_units(self):
434438
# String construction detecting units
435439
assert_equal(np.datetime64('2010').dtype,

0 commit comments

Comments
 (0)
0