Skip to content

Commit

Permalink
Merge pull request pandas-dev#5371 from jtratner/catch-errors-array-e…
Browse files Browse the repository at this point in the history
…qual

TST: Better handle np.array_equal() edge cases
  • Loading branch information
jtratner committed Oct 29, 2013
2 parents caee121 + 7368860 commit 6eba2e4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
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

0 comments on commit 6eba2e4

Please sign in to comment.