1+ import math
2+
13import numpy as np
24
35from pandas.core.dtypes.missing import isna, array_equivalent
@@ -38,13 +40,6 @@ cdef bint is_dictlike(obj):
3840 return hasattr (obj, ' keys' ) and hasattr (obj, ' __getitem__' )
3941
4042
41- cdef bint decimal_almost_equal(double desired, double actual, int decimal):
42- # Code from
43- # http://docs.scipy.org/doc/numpy/reference/generated
44- # /numpy.testing.assert_almost_equal.html
45- return abs (desired - actual) < (0.5 * 10.0 ** - decimal)
46-
47-
4843cpdef assert_dict_equal(a, b, bint compare_keys = True ):
4944 assert is_dictlike(a) and is_dictlike(b), (
5045 " Cannot compare dict objects, one or both is not dict-like"
@@ -63,7 +58,7 @@ cpdef assert_dict_equal(a, b, bint compare_keys=True):
6358
6459
6560cpdef assert_almost_equal(a, b,
66- check_less_precise = False ,
61+ rtol = 0.5e-5 , atol = 0.5e-5 ,
6762 bint check_dtype = True ,
6863 obj = None , lobj = None , robj = None ):
6964 """
@@ -73,25 +68,23 @@ cpdef assert_almost_equal(a, b,
7368 ----------
7469 a : object
7570 b : object
76- check_less_precise : bool or int, default False
77- Specify comparison precision.
78- 5 digits (False) or 3 digits (True) after decimal points are
79- compared. If an integer, then this will be the number of decimal
80- points to compare
71+ rtol : float, default 0.5e-5
72+ Relative tolerance.
73+ atol : float, default 0.5e-5
74+ Absolute tolerance.
8175 check_dtype: bool, default True
82- check dtype if both a and b are np.ndarray
76+ check dtype if both a and b are np.ndarray.
8377 obj : str, default None
8478 Specify object name being compared, internally used to show
85- appropriate assertion message
79+ appropriate assertion message.
8680 lobj : str, default None
8781 Specify left object name being compared, internally used to show
88- appropriate assertion message
82+ appropriate assertion message.
8983 robj : str, default None
9084 Specify right object name being compared, internally used to show
91- appropriate assertion message
85+ appropriate assertion message.
9286 """
9387 cdef:
94- int decimal
9588 double diff = 0.0
9689 Py_ssize_t i, na, nb
9790 double fa, fb
@@ -102,8 +95,6 @@ cpdef assert_almost_equal(a, b,
10295 if robj is None :
10396 robj = b
10497
105- assert isinstance (check_less_precise, (int , bool ))
106-
10798 if isinstance (a, dict ) or isinstance (b, dict ):
10899 return assert_dict_equal(a, b)
109100
@@ -161,8 +152,7 @@ cpdef assert_almost_equal(a, b,
161152
162153 for i in range (len (a)):
163154 try :
164- assert_almost_equal(a[i], b[i],
165- check_less_precise = check_less_precise)
155+ assert_almost_equal(a[i], b[i], rtol = rtol, atol = atol)
166156 except AssertionError :
167157 is_unequal = True
168158 diff += 1
@@ -194,24 +184,11 @@ cpdef assert_almost_equal(a, b,
194184 # inf comparison
195185 return True
196186
197- if check_less_precise is True :
198- decimal = 3
199- elif check_less_precise is False :
200- decimal = 5
201- else :
202- decimal = check_less_precise
203-
204187 fa, fb = a, b
205188
206- # case for zero
207- if abs (fa) < 1e-5 :
208- if not decimal_almost_equal(fa, fb, decimal):
209- assert False , (f' (very low values) expected {fb:.5f} '
210- f' but got {fa:.5f}, with decimal {decimal}' )
211- else :
212- if not decimal_almost_equal(1 , fb / fa, decimal):
213- assert False , (f' expected {fb:.5f} but got {fa:.5f}, '
214- f' with decimal {decimal}' )
189+ if not math.isclose(fa, fb, rel_tol = rtol, abs_tol = atol):
190+ assert False , (f" expected {fb:.5f} but got {fa:.5f}, "
191+ f" with rtol={rtol}, atol={atol}" )
215192 return True
216193
217194 raise AssertionError (f" {a} != {b}" )
0 commit comments