8000 BUG: assert_array_equal raises on NaN-s/NaT-s in records · Issue #21539 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: assert_array_equal raises on NaN-s/NaT-s in records #21539

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

Open
vmarkovtsev opened this issue May 19, 2022 · 2 comments · May be fixed by #21801
Open

BUG: assert_array_equal raises on NaN-s/NaT-s in records #21539

vmarkovtsev opened this issue May 19, 2022 · 2 comments · May be fixed by #21801
Labels

Comments

@vmarkovtsev
Copy link
vmarkovtsev commented May 19, 2022

Describe the issue:

assert_array_equal promises to correctly compare nan-s, but if there is a nan in a struct, it raises.

Reproduce the code example:

import numpy as np
from numpy.testing import assert_array_equal

arr = np.array([(1, np.timedelta64("NaT", "s"))], dtype=[("a", int), ("b", "timedelta64[s]")])
# same result: arr = np.array([(1, np.nan)], dtype=[("a", int), ("b", float)])
assert_array_equal(arr, arr)

Error message:

AssertionError: 
Arrays are not equal

Mismatched elements: 1 / 1 (100%)
 x: array([(1, 'NaT')], dtype=[('a', '<i8'), ('b', '<m8[s]')])
 y: array([(1, 'NaT')], dtype=[('a', '<i8'), ('b', '<m8[s]')])

NumPy/Python version information:

1.21.4

@vmarkovtsev vmarkovtsev changed the title BUG: assert_array_equal raises on NaT-s in records BUG: assert_array_equal raises on NaN-s/NaT-s in records May 19, 2022
@Micky774
Copy link
Contributor

Looks like, in general, assert_array_equal isn't set up well to handle comparisons of record arrays / arrays with structured dtypes. Working on this in #21801

@Thijss
Copy link
Thijss commented Sep 18, 2024

As a workaround I'm doing this, but that is of course less efficient.

def struct_array_equal_nan(array1, array2):
    for column in array1.dtype.names:
        if not np.array_equal(array1[column], array2[column], equal_nan=True):
            return False
    return True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0