Skip to content

TST: Better handle np.array_equal() edge cases #5371

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

Merged
merged 1 commit into from
Oct 29, 2013
Merged
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
10 changes: 6 additions & 4 deletions pandas/src/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False):
assert na == nb, (
"Length of two iterators not the same: %r != %r" % (na, nb)
)
if (isinstance(a, np.ndarray) and
isinstance(b, np.ndarray) and
np.array_equal(a, b)):
return True
if isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
try:
if np.array_equal(a, b):
return True
except:
pass
else:
for i in xrange(na):
assert_almost_equal(a[i], b[i], check_less_precise)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def test_assert_almost_equal_numbers_with_mixed(self):
self._assert_not_almost_equal_both(1, [1,])
self._assert_not_almost_equal_both(1, object())

def test_assert_almost_equal_edge_case_ndarrays(self):
self._assert_almost_equal_both(np.array([], dtype='M8[ns]'),
np.array([], dtype='float64'))
self._assert_almost_equal_both(np.array([], dtype=str),
np.array([], dtype='int64'))

def test_assert_almost_equal_dicts(self):
self._assert_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 2})

Expand Down
2 changes: 1 addition & 1 deletion pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ def assert_series_equal(left, right, check_dtype=True,
check_less_precise=False):
if check_series_type:
assert_isinstance(left, type(right))
assert_almost_equal(left.values, right.values, check_less_precise)
if check_dtype:
assert_attr_equal('dtype', left, right)
assert_almost_equal(left.values, right.values, check_less_precise)
if check_less_precise:
assert_almost_equal(
left.index.values, right.index.values, check_less_precise)
Expand Down