Skip to content

BUG: use cmath to test complex number equality in pandas._testing #36580

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 4 commits into from
Oct 1, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Numeric
- Bug in :meth:`DataFrame.any` with ``axis=1`` and ``bool_only=True`` ignoring the ``bool_only`` keyword (:issue:`32432`)
- Bug in :meth:`Series.equals` where a ``ValueError`` was raised when numpy arrays were compared to scalars (:issue:`35267`)
- Bug in :class:`Series` where two :class:`Series` each have a :class:`DatetimeIndex` with different timezones having those indexes incorrectly changed when performing arithmetic operations (:issue:`33671`)
- Bug in :meth:`pd._testing.assert_almost_equal` was incorrect for complex numeric types (:issue:`28235`)
-

Conversion
Expand Down
12 changes: 12 additions & 0 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cmath
import math

import numpy as np
Expand All @@ -7,6 +8,7 @@ from numpy cimport import_array
import_array()

from pandas._libs.util cimport is_array
from pandas._libs.lib import is_complex

from pandas.core.dtypes.common import is_dtype_equal
from pandas.core.dtypes.missing import array_equivalent, isna
Expand Down Expand Up @@ -210,4 +212,14 @@ cpdef assert_almost_equal(a, b,
f"with rtol={rtol}, atol={atol}")
return True

if is_complex(a) and is_complex(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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not cmath.isclose(a, b, rel_tol=rtol, abs_tol=atol):
if cmath.isclose(a, b, rel_tol=rtol, abs_tol=atol):

I think should just test for True here and return accordingly, otherwise let fall to the standard assertionerror, unless there is a reason for things to be different here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied what we do for reals, where we cite the rtol and atol if the comparison fails:

if not math.isclose(fa, fb, rel_tol=rtol, abs_tol=atol):
assert False, (f"expected {fb:.5f} but got {fa:.5f}, "
f"with rtol={rtol}, atol={atol}")

whereas the message at the bottom is just:
raise AssertionError(f"{a} != {b}")

assert False, (f"expected {b:.5f} but got {a:.5f}, "
f"with rtol={rtol}, atol={atol}")
return True

raise AssertionError(f"{a} != {b}")
31 changes: 31 additions & 0 deletions pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ def test_assert_not_almost_equal_numbers_rtol(a, b):
_assert_not_almost_equal_both(a, b, rtol=0.05)


@pytest.mark.parametrize(
"a,b,rtol",
[
(1.00001, 1.00005, 0.001),
(-0.908356 + 0.2j, -0.908358 + 0.2j, 1e-3),
(0.1 + 1.009j, 0.1 + 1.006j, 0.1),
(0.1001 + 2.0j, 0.1 + 2.001j, 0.01),
],
)
def test_assert_almost_equal_complex_numbers(a, b, rtol):
_assert_almost_equal_both(a, b, rtol=rtol)
_assert_almost_equal_both(np.complex64(a), np.complex64(b), rtol=rtol)
_assert_almost_equal_both(np.complex128(a), np.complex128(b), rtol=rtol)


@pytest.mark.parametrize(
"a,b,rtol",
[
(0.58310768, 0.58330768, 1e-7),
(-0.908 + 0.2j, -0.978 + 0.2j, 0.001),
(0.1 + 1j, 0.1 + 2j, 0.01),
(-0.132 + 1.001j, -0.132 + 1.005j, 1e-5),
(0.58310768j, 0.58330768j, 1e-9),
],
)
def test_assert_not_almost_equal_complex_numbers(a, b, rtol):
_assert_not_almost_equal_both(a, b, rtol=rtol)
_assert_not_almost_equal_both(np.complex64(a), np.complex64(b), rtol=rtol)
_assert_not_almost_equal_both(np.complex128(a), np.complex128(b), rtol=rtol)


@pytest.mark.parametrize("a,b", [(0, 0), (0, 0.0), (0, np.float64(0)), (0.00000001, 0)])
def test_assert_almost_equal_numbers_with_zeros(a, b):
_assert_almost_equal_both(a, b)
Expand Down