-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
950e0bf
cd86d6a
8f7e719
5c6a742
d006a56
489ee3d
8d5a49a
6905a59
9c4f3ed
9ee33c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, good idea |
||
if x is C_NA and y is C_NA: | ||
return True | ||
else: | ||
return False | ||
elif not (PyObject_RichCompareBool(x, y, Py_EQ) or | ||
(x is None or is_nan(x)) and (y is None or is_nan(y))): | ||
return False | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas._libs.missing import NA | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in tests you can import this from main pandas namespace |
||
|
||
from pandas import Timestamp | ||
import pandas._testing as tm | ||
|
||
|
@@ -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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice - can you also add a test using |
||
# https://github.com/pandas-dev/pandas/issues/31881 | ||
a = np.array([True, False]) | ||
b = np.array([True, NA]) | ||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
msg = """numpy array are different | ||
|
||
numpy array values are different \\(50.0 %\\) | ||
\\[left\\]: \\[True, False\\] | ||
\\[right\\]: \\[True, <NA>\\]""" | ||
|
||
with pytest.raises(AssertionError, match=msg): | ||
tm.assert_numpy_array_equal(a, b) |
There was a problem hiding this comment.
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