Skip to content

PERF: avoid non-cython in testing.pyx #51833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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}")

Expand All @@ -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):
Expand All @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down