From 584efb6f48b3ddb986ea2a3c9abbd2a3e12c392c Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 7 Mar 2023 16:22:18 -0800 Subject: [PATCH] PERF: avoid non-cython in testing.pyx --- pandas/_libs/testing.pyx | 21 +++++-------------- pandas/tests/util/test_assert_almost_equal.py | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index 5e1f9a2f723fb..ca19670f37710 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -7,17 +7,14 @@ from numpy cimport import_array import_array() +from pandas._libs.missing cimport checknull from pandas._libs.util cimport ( is_array, is_complex_object, is_real_number_object, ) -from pandas.core.dtypes.common import is_dtype_equal -from pandas.core.dtypes.missing import ( - array_equivalent, - isna, -) +from pandas.core.dtypes.missing import array_equivalent cdef bint isiterable(obj): @@ -133,7 +130,7 @@ cpdef assert_almost_equal(a, b, raise_assert_detail( obj, f"{obj} shapes are different", a.shape, b.shape) - if check_dtype and not is_dtype_equal(a.dtype, b.dtype): + if check_dtype and a.dtype != b.dtype: from pandas._testing import assert_attr_equal assert_attr_equal("dtype", a, b, obj=obj) @@ -181,12 +178,12 @@ cpdef assert_almost_equal(a, b, # classes can't be the same, to raise error assert_class_equal(a, b, obj=obj) - if isna(a) and isna(b): + if checknull(a) and checknull(b): # TODO: Should require same-dtype NA? # nan / None comparison return True - if isna(a) and not isna(b) or not isna(a) and isna(b): + if (checknull(a) and not checknull(b)) or (not checknull(a) and checknull(b)): # boolean value of pd.NA is ambiguous raise AssertionError(f"{a} != {b}") @@ -195,10 +192,6 @@ cpdef assert_almost_equal(a, b, return True if is_real_number_object(a) and is_real_number_object(b): - if array_equivalent(a, b, strict_nan=True): - # inf comparison - return True - fa, fb = a, b if not math.isclose(fa, fb, rel_tol=rtol, abs_tol=atol): @@ -207,10 +200,6 @@ cpdef assert_almost_equal(a, b, return True if is_complex_object(a) and is_complex_object(b): - if array_equivalent(a, b, strict_nan=True): - # inf comparison - return True - if not cmath.isclose(a, b, rel_tol=rtol, abs_tol=atol): assert False, (f"expected {b:.5f} but got {a:.5f}, " f"with rtol={rtol}, atol={atol}") diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index 987af3ee1e78e..0167e105efa23 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -45,7 +45,7 @@ def _assert_not_almost_equal(a, b, **kwargs): try: tm.assert_almost_equal(a, b, **kwargs) msg = f"{a} and {b} were approximately equal when they shouldn't have been" - pytest.fail(msg=msg) + pytest.fail(reason=msg) except AssertionError: pass