Skip to content

BUG: revert assert_numpy_array_equal to before c2ea8fb2 (accept non-ndarrays) #13355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
47 changes: 26 additions & 21 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down