diff options
author | Mark Wiebe <mwiebe@enthought.com> | 2011-06-07 13:21:12 -0500 |
---|---|---|
committer | Mark Wiebe <mwiebe@enthought.com> | 2011-06-07 13:21:12 -0500 |
commit | a8274369fcbc3332067a8555782cb40cc4684c83 (patch) | |
tree | 00bf53d4e3951cd9e9588edf9876925482dea2e0 /numpy/core/arrayprint.py | |
parent | fd9ef72daaa66803fd3109502fb584bbda6f407c (diff) | |
parent | 283b2e712bd52e6661f2dc338eb14caae2f5a8da (diff) | |
download | numpy-a8274369fcbc3332067a8555782cb40cc4684c83.tar.gz |
Merge branch 'datetime-fixes'
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r-- | numpy/core/arrayprint.py | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index d0b899901..8595d6f02 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -15,7 +15,7 @@ __docformat__ = 'restructuredtext' import sys import numerictypes as _nt from umath import maximum, minimum, absolute, not_equal, isnan, isinf -from multiarray import format_longfloat +from multiarray import format_longfloat, datetime_as_string from fromnumeric import ravel @@ -72,7 +72,8 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, - 'bool' - 'int' - - 'timeint' : a `numpy.timeinteger` + - 'timedelta' : a `numpy.timedelta64` + - 'datetime' : a `numpy.datetime64` - 'float' - 'longfloat' : 128-bit floats - 'complexfloat' @@ -83,7 +84,7 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, Other keys that can be used to set a group of types at once are:: - 'all' : sets all types - - 'int_kind' : sets 'int' and 'timeint' + - 'int_kind' : sets 'int' - 'float_kind' : sets 'float' and 'longfloat' - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat' - 'str_kind' : sets 'str' and 'numpystr' @@ -239,12 +240,13 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', formatdict = {'bool' : _boolFormatter, 'int' : IntegerFormat(data), - 'timeint' : str, 'float' : FloatFormat(data, precision, suppress_small), 'longfloat' : LongFloatFormat(precision), 'complexfloat' : ComplexFormat(data, precision, suppress_small), 'longcomplexfloat' : LongComplexFormat(precision), + 'datetime' : DatetimeFormat(True, None, -1), + 'timedelta' : TimedeltaFormat(data), 'numpystr' : repr, 'str' : str} if formatter is not None: @@ -253,7 +255,7 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', for key in formatdict.keys(): formatdict[key] = formatter['all'] if 'int_kind' in fkeys: - for key in ['int', 'timeint']: + for key in ['int']: formatdict[key] = formatter['int_kind'] if 'float_kind' in fkeys: for key in ['float', 'longfloat']: @@ -280,8 +282,8 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', if issubclass(dtypeobj, _nt.bool_): format_function = formatdict['bool'] elif issubclass(dtypeobj, _nt.integer): - if issubclass(dtypeobj, _nt.timeinteger): - format_function = formatdict['timeint'] + if issubclass(dtypeobj, _nt.timedelta64): + format_function = formatdict['timedelta'] else: format_function = formatdict['int'] elif issubclass(dtypeobj, _nt.floating): @@ -296,6 +298,8 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', format_function = formatdict['complexfloat'] elif issubclass(dtypeobj, (_nt.unicode_, _nt.string_)): format_function = formatdict['numpystr'] + elif issubclass(dtypeobj, _nt.datetime64): + format_function = formatdict['datetime'] else: format_function = formatdict['str'] @@ -361,7 +365,8 @@ def array2string(a, max_line_width=None, precision=None, - 'bool' - 'int' - - 'timeint' : a `numpy.timeinteger` + - 'timedelta' : a `numpy.timedelta64` + - 'datetime' : a `numpy.datetime64` - 'float' - 'longfloat' : 128-bit floats - 'complexfloat' @@ -372,7 +377,7 @@ def array2string(a, max_line_width=None, precision=None, Other keys that can be used to set a group of types at once are:: - 'all' : sets all types - - 'int_kind' : sets 'int' and 'timeint' + - 'int_kind' : sets 'int' - 'float_kind' : sets 'float' and 'longfloat' - 'complex_kind' : sets 'complexfloat' and 'longcomplexfloat' - 'str_kind' : sets 'str' and 'numpystr' @@ -691,3 +696,30 @@ class ComplexFormat(object): else: i = i + 'j' return r + i + +class DatetimeFormat(object): + def __init__(self, uselocaltime=True, overrideunit=None, tzoffset=-1): + self.local = uselocaltime + self.unit = overrideunit + self.tzoffset = -1 + + def __call__(self, x): + return "'%s'" % datetime_as_string(x, + local=self.local, + unit=self.unit, + tzoffset=self.tzoffset) + +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)))) + self.format = '%' + str(max_str_len) + 'd' + + def __call__(self, x): + if _MININT < x < _MAXINT: + return self.format % x.astype('i8') + else: + return "%s" % x + |