diff options
-rw-r--r-- | numpy/core/arrayprint.py | 27 | ||||
-rw-r--r-- | numpy/core/tests/test_datetime.py | 11 |
2 files changed, 32 insertions, 6 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 567573af5..508056a26 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, datetime_as_string +from multiarray import format_longfloat, datetime_as_string, datetime_data from fromnumeric import ravel @@ -245,7 +245,7 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', 'complexfloat' : ComplexFormat(data, precision, suppress_small), 'longcomplexfloat' : LongComplexFormat(precision), - 'datetime' : DatetimeFormat(), + 'datetime' : DatetimeFormat(data), 'timedelta' : TimedeltaFormat(data), 'numpystr' : repr, 'str' : str} @@ -698,10 +698,25 @@ class ComplexFormat(object): return r + i class DatetimeFormat(object): - def __init__(self, overrideunit=None, - timezone='local', casting='same_kind'): - self.timezone = timezone - self.unit = overrideunit + def __init__(self, x, unit=None, + timezone=None, casting='same_kind'): + # Get the unit from the dtype + if unit is None: + if x.dtype.kind == 'M': + unit = datetime_data(x.dtype)[0] + else: + unit = 's' + + # If timezone is default, make it 'local' or 'UTC' based on the unit + if timezone is None: + # Date units -> UTC, time units -> local + if unit in ('Y', 'M', 'W', 'D'): + self.timezone = 'UTC' + else: + self.timezone = 'local' + else: + self.timezone = timezone + self.unit = unit self.casting = casting def __call__(self, x): diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index f97592370..9af00a81b 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -475,6 +475,17 @@ class TestDateTime(TestCase): str_b[...] = dt_a assert_equal(str_a, str_b) + def test_datetime_array_str(self): + a = np.array(['2011-03-16', '1920-01-01', '2013-05-19'], dtype='M') + assert_equal(str(a), "['2011-03-16' '1920-01-01' '2013-05-19']") + + a = np.array(['2011-03-16T13:55Z', '1920-01-01T03:12Z'], dtype='M') + assert_equal(np.array2string(a, separator=', ', + formatter={'datetime': lambda x : + "'%s'" % np.datetime_as_string(x, timezone='UTC')}), + "['2011-03-16T13:55Z', '1920-01-01T03:12Z']") + + def test_pickle(self): # Check that pickle roundtripping works dt = np.dtype('M8[7D]') |