summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2016-06-19 13:39:46 +0200
committerSebastian Berg <sebastian@sipsolutions.net>2016-09-02 10:10:55 +0200
commit86b0a5e9c58160bad818ba3a0a6faf38f5ac4d09 (patch)
tree928ce2677bcdf6735ebac42955859f5d522200b1 /numpy/core
parent968507bdfb4467d5ec6e3b6999a5717100782c3c (diff)
downloadnumpy-86b0a5e9c58160bad818ba3a0a6faf38f5ac4d09.tar.gz
BUG: Suppress common NaT warnings
Printing of datetime arrays used to cause warning due to comparison warnings in NaT. This is circumvented by using views to integer values. Part of this should be simplified when the deprecation is over. Also fixes a bug with non-native byteorder.
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/arrayprint.py9
-rw-r--r--numpy/core/tests/test_datetime.py72
2 files changed, 47 insertions, 34 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index 282fbd1cf..b05082e9d 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -20,7 +20,7 @@ from functools import reduce
from . import numerictypes as _nt
from .umath import maximum, minimum, absolute, not_equal, isnan, isinf
from .multiarray import (array, format_longfloat, datetime_as_string,
- datetime_data)
+ datetime_data, dtype)
from .fromnumeric import ravel
from .numeric import asarray
@@ -734,7 +734,9 @@ class TimedeltaFormat(object):
def __init__(self, data):
if data.dtype.kind == 'm':
nat_value = array(['NaT'], dtype=data.dtype)[0]
- v = data[not_equal(data, nat_value)].view('i8')
+ 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:
# Max str length of non-NaT elements
max_str_len = max(len(str(maximum.reduce(v))),
@@ -748,7 +750,8 @@ class TimedeltaFormat(object):
self._nat = "'NaT'".rjust(max_str_len)
def __call__(self, x):
- if x + 1 == x:
+ # TODO: After NAT == NAT deprecation should be simplified:
+ if (x + 1).view('i8') == x.view('i8'):
return self._nat
else:
return self.format % x.astype('i8')
diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py
index 601f09c09..e443b3be0 100644
--- a/numpy/core/tests/test_datetime.py
+++ b/numpy/core/tests/test_datetime.py
@@ -1,7 +1,6 @@
from __future__ import division, absolute_import, print_function
import pickle
-import warnings
import numpy
import numpy as np
@@ -9,7 +8,7 @@ import datetime
from numpy.compat import asbytes
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_raises,
- assert_warns, dec
+ assert_warns, dec, suppress_warnings
)
# Use pytz to test out various time zones if available
@@ -129,10 +128,11 @@ class TestDateTime(TestCase):
# regression tests for GH6452
assert_equal(np.datetime64('NaT'),
np.datetime64('2000') + np.timedelta64('NaT'))
- # nb. we may want to make NaT != NaT true in the future; this test
- # verifies the existing behavior (and that it should not warn)
- assert_(np.datetime64('NaT') == np.datetime64('NaT', 'us'))
- assert_(np.datetime64('NaT', 'us') == np.datetime64('NaT'))
+ # nb. we may want to make NaT != NaT true in the future
+ with suppress_warnings() as sup:
+ sup.filter(FutureWarning, ".*NAT ==")
+ assert_(np.datetime64('NaT') == np.datetime64('NaT', 'us'))
+ assert_(np.datetime64('NaT', 'us') == np.datetime64('NaT'))
def test_datetime_scalar_construction(self):
# Construct with different units
@@ -572,6 +572,12 @@ class TestDateTime(TestCase):
a = np.array([-1, 'NaT', 1234567], dtype='m')
assert_equal(str(a), "[ -1 'NaT' 1234567]")
+ # Test with other byteorder:
+ a = np.array([-1, 'NaT', 1234567], dtype='>m')
+ assert_equal(str(a), "[ -1 'NaT' 1234567]")
+ a = np.array([-1, 'NaT', 1234567], dtype='<m')
+ assert_equal(str(a), "[ -1 'NaT' 1234567]")
+
def test_pickle(self):
# Check that pickle roundtripping works
dt = np.dtype('M8[7D]')
@@ -989,8 +995,8 @@ class TestDateTime(TestCase):
assert_raises(TypeError, np.multiply, 1.5, dta)
# NaTs
- with warnings.catch_warnings():
- warnings.filterwarnings('ignore', category=RuntimeWarning)
+ with suppress_warnings() as sup:
+ sup.filter(RuntimeWarning, "invalid value encountered in multiply")
nat = np.timedelta64('NaT')
def check(a, b, res):
assert_equal(a * b, res)
@@ -1053,8 +1059,8 @@ class TestDateTime(TestCase):
assert_raises(TypeError, np.divide, 1.5, dta)
# NaTs
- with warnings.catch_warnings():
- warnings.filterwarnings('ignore', category=RuntimeWarning)
+ with suppress_warnings() as sup:
+ sup.filter(RuntimeWarning, r".*encountered in true\_divide")
nat = np.timedelta64('NaT')
for tp in (int, float):
assert_equal(np.timedelta64(1) / tp(0), nat)
@@ -1092,27 +1098,31 @@ class TestDateTime(TestCase):
td_nat = np.timedelta64('NaT', 'h')
td_other = np.timedelta64(1, 'h')
- for op in [np.equal, np.less, np.less_equal,
- np.greater, np.greater_equal]:
- if op(dt_nat, dt_nat):
- assert_warns(FutureWarning, op, dt_nat, dt_nat)
- if op(dt_nat, dt_other):
- assert_warns(FutureWarning, op, dt_nat, dt_other)
- if op(dt_other, dt_nat):
- assert_warns(FutureWarning, op, dt_other, dt_nat)
- if op(td_nat, td_nat):
- assert_warns(FutureWarning, op, td_nat, td_nat)
- if op(td_nat, td_other):
- assert_warns(FutureWarning, op, td_nat, td_other)
- if op(td_other, td_nat):
- assert_warns(FutureWarning, op, td_other, td_nat)
-
- assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat)
- assert_(np.not_equal(dt_nat, dt_other))
- assert_(np.not_equal(dt_other, dt_nat))
- assert_warns(FutureWarning, np.not_equal, td_nat, td_nat)
- assert_(np.not_equal(td_nat, td_other))
- assert_(np.not_equal(td_other, td_nat))
+ with suppress_warnings() as sup:
+ # The assert warns contexts will again see the warning:
+ sup.filter(FutureWarning, ".*NAT")
+
+ for op in [np.equal, np.less, np.less_equal,
+ np.greater, np.greater_equal]:
+ if op(dt_nat, dt_nat):
+ assert_warns(FutureWarning, op, dt_nat, dt_nat)
+ if op(dt_nat, dt_other):
+ assert_warns(FutureWarning, op, dt_nat, dt_other)
+ if op(dt_other, dt_nat):
+ assert_warns(FutureWarning, op, dt_other, dt_nat)
+ if op(td_nat, td_nat):
+ assert_warns(FutureWarning, op, td_nat, td_nat)
+ if op(td_nat, td_other):
+ assert_warns(FutureWarning, op, td_nat, td_other)
+ if op(td_other, td_nat):
+ assert_warns(FutureWarning, op, td_other, td_nat)
+
+ assert_warns(FutureWarning, np.not_equal, dt_nat, dt_nat)
+ assert_(np.not_equal(dt_nat, dt_other))
+ assert_(np.not_equal(dt_other, dt_nat))
+ assert_warns(FutureWarning, np.not_equal, td_nat, td_nat)
+ assert_(np.not_equal(td_nat, td_other))
+ assert_(np.not_equal(td_other, td_nat))
def test_datetime_minmax(self):
# The metadata of the result should become the GCD