Skip to content

Commit

Permalink
Merge pull request #17774 from charris/bacport-17645
Browse files Browse the repository at this point in the history
BUG: fix np.timedelta64('nat').__format__ throwing an exception
  • Loading branch information
charris committed Nov 15, 2020
2 parents 760449c + e3546d3 commit 672a6c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion numpy/core/src/multiarray/scalartypes.c.src
Expand Up @@ -275,7 +275,8 @@ gentype_format(PyObject *self, PyObject *args)
if (Py_TYPE(self) == &PyBoolArrType_Type) {
obj = PyBool_FromLong(PyArrayScalar_VAL(self, Bool));
}
else if (PyArray_IsScalar(self, Integer)) {
else if (PyArray_IsScalar(self, Integer)
&& !PyArray_IsScalar(self, Timedelta)) {
obj = Py_TYPE(self)->tp_as_number->nb_int(self);
}
else if (PyArray_IsScalar(self, Floating)) {
Expand Down
4 changes: 4 additions & 0 deletions numpy/core/tests/test_datetime.py
Expand Up @@ -429,6 +429,10 @@ def test_timedelta_0_dim_object_array_conversion(self):
np.timedelta64)
assert_equal(actual, expected)

def test_timedelta_nat_format(self):
# gh-17552
assert_equal('NaT', '{0}'.format(np.timedelta64('nat')))

def test_timedelta_scalar_construction_units(self):
# String construction detecting units
assert_equal(np.datetime64('2010').dtype,
Expand Down

0 comments on commit 672a6c8

Please sign in to comment.