Skip to content

BUG: Handle NA in assert_numpy_array_equal #31910

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 10 commits into from
Feb 13, 2020
Merged

BUG: Handle NA in assert_numpy_array_equal #31910

merged 10 commits into from
Feb 13, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented Feb 12, 2020

I think in the the case where we have NA we want to use identity rather than equality for checking when two arrays are equal.

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.

Awesome thanks for taking a look!

@@ -571,6 +571,11 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:
if PyArray_Check(x) and PyArray_Check(y):
if not array_equivalent_object(x, y):
return False
elif x is C_NA or y is C_NA:
Copy link
Member

Choose a reason for hiding this comment

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

I think you can just simplify by doing an XOR and returning False when that is the case. PyObject_RichCompareBool on the next condition will return True when both objects have the same id anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes, good idea

@@ -32,6 +32,10 @@ Bug fixes

- Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`)

**Testing**

- Fixed bug where :meth:`assert_numpy_array_equal` would raise a ``TypeError`` when encountering ``pd.NA`` (:issue:`31881`)
Copy link
Member

Choose a reason for hiding this comment

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

This one actually isn't exposed via API so can remove note

@@ -175,3 +177,18 @@ def test_numpy_array_equal_copy_flag(other_type, check_same):
tm.assert_numpy_array_equal(a, other, check_same=check_same)
else:
tm.assert_numpy_array_equal(a, other, check_same=check_same)


def test_numpy_array_equal_contains_na():
Copy link
Member

Choose a reason for hiding this comment

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

Nice - can you also add a test using nulls_fixture that ensures equality passes?

@WillAyd WillAyd added the Testing pandas testing functions or related to the test suite label Feb 12, 2020
def test_numpy_array_equal_identical_na(nulls_fixture):
a = np.array([nulls_fixture])

tm.assert_numpy_array_equal(a, a)
Copy link
Member

Choose a reason for hiding this comment

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

Different NA-likes are not considered equal? Eg np.array([np.nan]) vs np.array([pd.NA]) ?

I think for now it would be good to keep those not equal (since they behave differently), but if so, we should best add a test for that as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, those are comparing unequal, will add a test

@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas._libs.missing import NA
Copy link
Member

Choose a reason for hiding this comment

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

in tests you can import this from main pandas namespace

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 outside of any @jorisvandenbossche comments

@WillAyd
Copy link
Member

WillAyd commented Feb 12, 2020

I think worth back porting to 1.0.2

@jreback jreback added this to the 1.1 milestone Feb 12, 2020
@jorisvandenbossche jorisvandenbossche modified the milestones: 1.1, 1.0.2 Feb 12, 2020
@jorisvandenbossche
Copy link
Member

I think worth back porting to 1.0.2

Yes, it's internal only, but since other fixes that will be backported might want to rely on this testing functionality, let's backport

@jorisvandenbossche jorisvandenbossche merged commit 56cc7f4 into pandas-dev:master Feb 13, 2020
@jorisvandenbossche
Copy link
Member

@dsaxton Thanks!

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

Successfully merging this pull request may close these issues.

assert_numpy_array_equal raises TypeError for pd.NA
4 participants