From 53ad5b1cb30ed8a5284549e98b4987d2f42f2973 Mon Sep 17 00:00:00 2001 From: Pietro Battiston Date: Fri, 3 Jun 2016 10:41:16 +0200 Subject: [PATCH] BUG: revert assert_numpy_array_equal to before c2ea8fb2 (accept non-ndarrays) --- pandas/tests/test_testing.py | 2 +- pandas/util/testing.py | 47 ++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/pandas/tests/test_testing.py b/pandas/tests/test_testing.py index c4e864a909c03..0ad0930862866 100644 --- a/pandas/tests/test_testing.py +++ b/pandas/tests/test_testing.py @@ -186,7 +186,7 @@ def test_numpy_array_equal_message(self): assert_almost_equal(np.array([1, 2]), np.array([3, 4, 5])) # scalar comparison - expected = """Expected type """ + expected = """: 1 != 2""" with assertRaisesRegexp(AssertionError, expected): assert_numpy_array_equal(1, 2) expected = """expected 2\\.00000 but got 1\\.00000, with decimal 5""" diff --git a/pandas/util/testing.py b/pandas/util/testing.py index 03ccfcab24f58..b457b8546e7ba 100644 --- a/pandas/util/testing.py +++ b/pandas/util/testing.py @@ -30,6 +30,7 @@ needs_i8_conversion, is_categorical_dtype) from pandas.formats.printing import pprint_thing from pandas.core.algorithms import take_1d +from pandas.lib import isscalar import pandas.compat as compat from pandas.compat import( @@ -1009,29 +1010,33 @@ def assert_numpy_array_equal(left, right, strict_nan=False, assertion message """ - # instance validation - # to show a detailed erorr message when classes are different - assert_class_equal(left, right, obj=obj) - # both classes must be an np.ndarray - assertIsInstance(left, np.ndarray, '[ndarray] ') - assertIsInstance(right, np.ndarray, '[ndarray] ') - def _raise(left, right, err_msg): if err_msg is None: - if left.shape != right.shape: - raise_assert_detail(obj, '{0} shapes are different' - .format(obj), left.shape, right.shape) - - diff = 0 - for l, r in zip(left, right): - # count up differences - if not array_equivalent(l, r, strict_nan=strict_nan): - diff += 1 - - diff = diff * 100.0 / left.size - msg = '{0} values are different ({1} %)'\ - .format(obj, np.round(diff, 5)) - raise_assert_detail(obj, msg, left, right) + # show detailed error + if isscalar(left) and isscalar(right): + # show scalar comparison error + assert_equal(left, right) + elif is_list_like(left) and is_list_like(right): + # some test cases pass list + left = np.asarray(left) + right = np.array(right) + + if left.shape != right.shape: + raise_assert_detail(obj, '{0} shapes are different' + .format(obj), left.shape, right.shape) + + diff = 0 + for l, r in zip(left, right): + # count up differences + if not array_equivalent(l, r, strict_nan=strict_nan): + diff += 1 + + diff = diff * 100.0 / left.size + msg = '{0} values are different ({1} %)'\ + .format(obj, np.round(diff, 5)) + raise_assert_detail(obj, msg, left, right) + else: + assert_class_equal(left, right, obj=obj) raise AssertionError(err_msg)