summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/arrayprint.py18
-rw-r--r--numpy/core/tests/test_arrayprint.py25
2 files changed, 27 insertions, 16 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index d57d76368..8399a47b2 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -39,7 +39,7 @@ else:
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 @@ class DatetimeFormat(object):
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')
diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py
index baaa1263d..32c96221d 100644
--- a/numpy/core/tests/test_arrayprint.py
+++ b/numpy/core/tests/test_arrayprint.py
@@ -172,11 +172,23 @@ class TestArray2String(object):
# 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 @@ class TestPrintOptions(object):
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"})