Skip to content

Commit 6eba2e4

Browse files
committed
Merge pull request #5371 from jtratner/catch-errors-array-equal
TST: Better handle np.array_equal() edge cases
2 parents caee121 + 7368860 commit 6eba2e4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

pandas/src/testing.pyx

+6-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ cpdef assert_almost_equal(a, b, bint check_less_precise=False):
8282
assert na == nb, (
8383
"Length of two iterators not the same: %r != %r" % (na, nb)
8484
)
85-
if (isinstance(a, np.ndarray) and
86-
isinstance(b, np.ndarray) and
87-
np.array_equal(a, b)):
88-
return True
85+
if isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
86+
try:
87+
if np.array_equal(a, b):
88+
return True
89+
except:
90+
pass
8991
else:
9092
for i in xrange(na):
9193
assert_almost_equal(a[i], b[i], check_less_precise)

pandas/tests/test_testing.py

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def test_assert_almost_equal_numbers_with_mixed(self):
4848
self._assert_not_almost_equal_both(1, [1,])
4949
self._assert_not_almost_equal_both(1, object())
5050

51+
def test_assert_almost_equal_edge_case_ndarrays(self):
52+
self._assert_almost_equal_both(np.array([], dtype='M8[ns]'),
53+
np.array([], dtype='float64'))
54+
self._assert_almost_equal_both(np.array([], dtype=str),
55+
np.array([], dtype='int64'))
56+
5157
def test_assert_almost_equal_dicts(self):
5258
self._assert_almost_equal_both({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
5359

pandas/util/testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ def assert_series_equal(left, right, check_dtype=True,
411411
check_less_precise=False):
412412
if check_series_type:
413413
assert_isinstance(left, type(right))
414-
assert_almost_equal(left.values, right.values, check_less_precise)
415414
if check_dtype:
416415
assert_attr_equal('dtype', left, right)
416+
assert_almost_equal(left.values, right.values, check_less_precise)
417417
if check_less_precise:
418418
assert_almost_equal(
419419
left.index.values, right.index.values, check_less_precise)

0 commit comments

Comments
 (0)