summaryrefslogtreecommitdiff
path: root/numpy/core/arrayprint.py
diff options
context:
space:
mode:
authorAntoine Pitrou <antoine@python.org>2015-07-01 19:54:50 +0200
committerAntoine Pitrou <antoine@python.org>2015-07-01 19:54:50 +0200
commit7aa4f494e3dbd183d08d5130b52c0c62fa2af675 (patch)
tree1cbb56fed83a60eaa92c97f57a964006562c77b6 /numpy/core/arrayprint.py
parent23e10e18f7951fe040c91134da894f6efdffebe6 (diff)
downloadnumpy-7aa4f494e3dbd183d08d5130b52c0c62fa2af675.tar.gz
ENH: improve string representation of NaTs in timedelta64 arrays
Rather than representing them as "-9223372036854775808", we now represent them as "'NaT'", as in datetime64 arrays. Note this changes the numpy.core.arrayprint printer, not the builtin printer.
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r--numpy/core/arrayprint.py23
1 files changed, 18 insertions, 5 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index 4a9765639..b8acaee97 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -19,7 +19,8 @@ import sys
from functools import reduce
from . import numerictypes as _nt
from .umath import maximum, minimum, absolute, not_equal, isnan, isinf
-from .multiarray import format_longfloat, datetime_as_string, datetime_data
+from .multiarray import (array, format_longfloat, datetime_as_string,
+ datetime_data)
from .fromnumeric import ravel
from .numeric import asarray
@@ -733,10 +734,22 @@ class DatetimeFormat(object):
class TimedeltaFormat(object):
def __init__(self, data):
if data.dtype.kind == 'm':
- v = data.view('i8')
- max_str_len = max(len(str(maximum.reduce(v))),
- len(str(minimum.reduce(v))))
+ nat_value = array(['NaT'], dtype=data.dtype)[0]
+ v = data[not_equal(data, nat_value)].view('i8')
+ if len(v) > 0:
+ # Max str length of non-NaT elements
+ max_str_len = max(len(str(maximum.reduce(v))),
+ len(str(minimum.reduce(v))))
+ else:
+ max_str_len = 0
+ if len(v) < len(data):
+ # 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):
- return self.format % x.astype('i8')
+ if x + 1 == x:
+ return self._nat
+ else:
+ return self.format % x.astype('i8')