diff options
author | Jaime <jaime.frio@gmail.com> | 2015-07-01 14:13:36 -0700 |
---|---|---|
committer | Jaime <jaime.frio@gmail.com> | 2015-07-01 14:13:36 -0700 |
commit | 61d2a445881f80b52bc9facdbd4f58f6e74c637b (patch) | |
tree | 1cbb56fed83a60eaa92c97f57a964006562c77b6 /numpy/core/arrayprint.py | |
parent | 23e10e18f7951fe040c91134da894f6efdffebe6 (diff) | |
parent | 7aa4f494e3dbd183d08d5130b52c0c62fa2af675 (diff) | |
download | numpy-61d2a445881f80b52bc9facdbd4f58f6e74c637b.tar.gz |
Merge pull request #6033 from pitrou/td64_array_repr
ENH: improve string representation of NaTs in timedelta64 arrays
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r-- | numpy/core/arrayprint.py | 23 |
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') |