Repository: numpy/numpy

Issue:
BUG: Repr of 0d timedelta fails
```python
>>> np.array(np.timedelta64('NaT'))
~\Repos\numpy\build\testenv\Lib\site-packages\numpy\core\arrayprint.py in __init__(self, data)
   1086         else:
   1087             max_str_len = 0
-> 1088         if len(v) < len(data):
   1089             # data contains a NaT
   1090             max_str_len = max(max_str_len, 5)

TypeError: len() of unsized object
```

Replacing with `data.size` should do the trick. We could also probably use `isnat` in this function.


Discussion:

Resolving patch:
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -39,7 +39,7 @@
 
 import numpy as np
 from . import numerictypes as _nt
-from .umath import absolute, not_equal, isnan, isinf, isfinite
+from .umath import absolute, not_equal, isnan, isinf, isfinite, isnat
 from . import multiarray
 from .multiarray import (array, dragon4_positional, dragon4_scientific,
                          datetime_as_string, datetime_data, dtype, ndarray,
@@ -1075,25 +1075,21 @@ def __call__(self, x):
 
 class TimedeltaFormat(object):
     def __init__(self, data):
-        nat_value = array(['NaT'], dtype=data.dtype)[0]
-        int_dtype = dtype(data.dtype.byteorder + 'i8')
-        int_view = data.view(int_dtype)
-        v = int_view[not_equal(int_view, nat_value.view(int_dtype))]
-        if len(v) > 0:
+        non_nat = data[~isnat(data)]
+        if len(non_nat) > 0:
             # Max str length of non-NaT elements
-            max_str_len = max(len(str(np.max(v))),
-                              len(str(np.min(v))))
+            max_str_len = max(len(str(np.max(non_nat).astype('i8'))),
+                              len(str(np.min(non_nat).astype('i8'))))
         else:
             max_str_len = 0
-        if len(v) < len(data):
+        if len(non_nat) < data.size:
             # data contains a NaT
             max_str_len = max(max_str_len, 5)
         self.format = '%' + str(max_str_len) + 'd'
         self._nat = "'NaT'".rjust(max_str_len)
 
     def __call__(self, x):
-        # TODO: After NAT == NAT deprecation should be simplified:
-        if (x + 1).view('i8') == x.view('i8'):
+        if isnat(x):
             return self._nat
         else:
             return self.format % x.astype('i8')


Test patch:
diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py
--- a/numpy/core/tests/test_arrayprint.py
+++ b/numpy/core/tests/test_arrayprint.py
@@ -172,11 +172,23 @@ def test_structure_format(self):
         # for issue #5692
         A = np.zeros(shape=10, dtype=[("A", "M8[s]")])
         A[5:].fill(np.datetime64('NaT'))
-        assert_equal(np.array2string(A),
-                "[('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) " +
-                "('1970-01-01T00:00:00',)\n ('1970-01-01T00:00:00',) " +
-                "('1970-01-01T00:00:00',) ('NaT',) ('NaT',)\n " +
-                "('NaT',) ('NaT',) ('NaT',)]")
+        assert_equal(
+            np.array2string(A),
+            textwrap.dedent("""\
+            [('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',)
+             ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('NaT',) ('NaT',)
+             ('NaT',) ('NaT',) ('NaT',)]""")
+        )
+
+        # and again, with timedeltas
+        A = np.full(10, 123456, dtype=[("A", "m8[s]")])
+        A[5:].fill(np.datetime64('NaT'))
+        assert_equal(
+            np.array2string(A),
+            textwrap.dedent("""\
+            [(123456,) (123456,) (123456,) (123456,) (123456,) ( 'NaT',) ( 'NaT',)
+             ( 'NaT',) ( 'NaT',) ( 'NaT',)]""")
+        )
 
         # See #8160
         struct_int = np.array([([1, -1],), ([123, 1],)], dtype=[('B', 'i4', 2)])
@@ -276,6 +288,9 @@ def test_0d_arrays(self):
         assert_equal(repr(np.datetime64('2005-02-25')[...]),
                      "array('2005-02-25', dtype='datetime64[D]')")
 
+        assert_equal(repr(np.timedelta64('10', 'Y')[...]),
+                     "array(10, dtype='timedelta64[Y]')")
+
         # repr of 0d arrays is affected by printoptions
         x = np.array(1)
         np.set_printoptions(formatter={'all':lambda x: "test"})