Skip to content

Commit be6d747

Browse files
committed
test complex number equality with cmath
1 parent 9628816 commit be6d747

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pandas/_libs/testing.pyx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cmath
12
import math
23

34
import numpy as np
@@ -31,10 +32,21 @@ cdef NUMERIC_TYPES = (
3132
)
3233

3334

35+
cdef COMPLEX_NUMERIC_TYPES = (
36+
complex,
37+
np.complex64,
38+
np.complex128,
39+
)
40+
41+
3442
cdef bint is_comparable_as_number(obj):
3543
return isinstance(obj, NUMERIC_TYPES)
3644

3745

46+
cdef bint is_comparable_as_complex_number(obj):
47+
return isinstance(obj, COMPLEX_NUMERIC_TYPES)
48+
49+
3850
cdef bint isiterable(obj):
3951
return hasattr(obj, '__iter__')
4052

@@ -210,4 +222,14 @@ cpdef assert_almost_equal(a, b,
210222
f"with rtol={rtol}, atol={atol}")
211223
return True
212224

225+
if is_comparable_as_complex_number(a) and is_comparable_as_complex_number(b):
226+
if array_equivalent(a, b, strict_nan=True):
227+
# inf comparison
228+
return True
229+
230+
if not cmath.isclose(a, b, rel_tol=rtol, abs_tol=atol):
231+
assert False, (f"expected {b:.5f} but got {a:.5f}, "
232+
f"with rtol={rtol}, atol={atol}")
233+
return True
234+
213235
raise AssertionError(f"{a} != {b}")

0 commit comments

Comments
 (0)