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

Conversation

arw2019
Copy link
Member

@arw2019 arw2019 commented Sep 23, 2020

Adds cmath-based equality testing for complex numeric types (complex, np.complex64 and np.complex128)

cdef bint is_comparable_as_number(obj):
return isinstance(obj, NUMERIC_TYPES)


cdef bint is_comparable_as_complex_number(obj):
return isinstance(obj, COMPLEX_NUMERIC_TYPES)
Copy link
Member

Choose a reason for hiding this comment

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

we have lib.is_complex for this

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Switched to using that

@jreback jreback added the Complex Complex Numbers label Sep 24, 2020
@jreback jreback modified the milestone: 1.2 Sep 24, 2020
@arw2019
Copy link
Member Author

arw2019 commented Sep 24, 2020

Travis failure looks unrelated

# 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}")

@jreback jreback added this to the 1.2 milestone Sep 24, 2020
@jreback jreback added the Testing pandas testing functions or related to the test suite label Sep 24, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

can you add a whatsnew note, bug fixes in numeric for 1.2 (and @WillAyd comments); ping on green.

@arw2019
Copy link
Member Author

arw2019 commented Sep 25, 2020

Green barring unrelated doctest failure:

=================================== FAILURES ===================================
_________________ [doctest] pandas.core.reshape.pivot.crosstab _________________
549     Examples
550     --------
551     >>> a = np.array(["foo", "foo", "foo", "foo", "bar", "bar",
552     ...               "bar", "bar", "foo", "foo", "foo"], dtype=object)
553     >>> b = np.array(["one", "one", "one", "two", "one", "one",
554     ...               "one", "two", "two", "two", "one"], dtype=object)
555     >>> c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny",
556     ...               "shiny", "dull", "shiny", "shiny", "shiny"],
557     ...              dtype=object)
558     >>> pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'])
Differences (unified diff with -expected +actual):
    @@ -1,5 +1,6 @@
    -b   one        two
    +Index(['__dummy__'], dtype='object')
    +b    one        two      
     c   dull shiny dull shiny
    -a
    +a                        
     bar    1     2    1     0
     foo    2     2    1     2

/home/runner/work/pandas/pandas/pandas/core/reshape/pivot.py:558: DocTestFailure

@arw2019 arw2019 force-pushed the GH28235 branch 3 times, most recently from 46e4579 to 4e9fa93 Compare September 29, 2020 03:31
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm

@WillAyd WillAyd merged commit 39c6957 into pandas-dev:master Oct 1, 2020
@WillAyd
Copy link
Member

WillAyd commented Oct 1, 2020

Thanks @arw2019

@arw2019
Copy link
Member Author

arw2019 commented Oct 1, 2020

Thanks @WillAyd @jreback @jbrockmendel for reviewing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Complex Complex Numbers Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pandas._lib.testing.assert_almost_equal seem to not use approximate equality for Series with complex doubles
4 participants