8000 Structured arrays containing NaNs are not considered equal by numpy.testing.assert_array_equal() · Issue #8192 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Structured arrays containing NaNs are not considered equal by numpy.testing.assert_array_equal() #8192

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

Closed
colinpalmer opened this issue Oct 21, 2016 · 7 comments

Comments

@colinpalmer
Copy link

numpy.testing.assert_array_equal is supposed to accept NaN values as equal. This seems to work fine for normal arrays but not for arrays with structured dtypes:

>>> import numpy as np
>>> a = np.array((np.nan,), dtype=[('f', 'f4')])
>>> b = a.copy()
>>> np.testing.assert_array_equal(a, b)
Traceback (most recent call last):
  ...
AssertionError: 
Arrays are not equal

(mismatch 100.0%)
 x: array((nan,), 
      dtype=[('f', '<f4')])
 y: array((nan,), 
      dtype=[('f', '<f4')])

The result from assert_equal is identical. Tested using numpy 1.11.2 on python 2.7.12 and 3.5.2.

@charris
Copy link
Member
charris commented Oct 21, 2016

Hmm, doesn't work for assert_allclose either.

@charris charris added this to the 1.13.0 release milestone Oct 21, 2016
@shoyer
Copy link
Member
shoyer commented Oct 21, 2016

Note that np.isnan gives a TypeError on structured arrays: TypeError: ufunc 'isnan' not supported for the input types.... That might be related to the ultimate cause here.

@eric-wieser
Copy link
Member

Related to #9023

@charris
Copy link
Member
charris commented May 5, 2017

Pushing this off, it should get fixed when #8157 goes in.

@sethtroisi
Copy link
Contributor
sethtroisi commented Sep 16, 2019

This is still broken in 1.16.4

Doesn't seem to have been fixed by #8157

import numpy as np

a = np.array((np.nan,), dtype=[('f', 'f4')])
b = a.copy()

np.testing.assert_array_equal(a, b)
Traceback (most recent call last):
  File "t.py", line 7, in <module>
    np.testing.assert_array_equal(a, b)
  File "/usr/local/lib/python3.6/dist-packages/numpy/testing/_private/utils.py", line 904, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "/usr/local/lib/python3.6/dist-packages/numpy/testing/_private/utils.py", line 827, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal

Mismatch: 100%
 x: array((nan,), dtype=[('f', '<f4')])
 y: array((nan,), dtype=[('f', '<f4')])

shell returned 1

Press ENTER or type command to continue

@seberg
Copy link
Member
seberg commented Sep 16, 2019

We would have to define a new equivalent ufunc or so to do this correctly (i.e. combine isnan and equality check into one function). Then you could define this function also for structured dtypes. As Eric pointed out, the same issue exists for object dtypes.

mj-will added a commit to mj-will/nessai that referenced this issue Jun 17, 2022
* Includes tests
* Needed because of this issue in numpy: numpy/numpy#8192
mj-will added a commit to mj-will/nessai that referenced this issue Jun 20, 2022
* Includes tests
* Needed because of this issue in numpy: numpy/numpy#8192
mj-will added a commit to mj-will/nessai that referenced this issue Jun 21, 2022
* Includes tests
* Needed because of this issue in numpy: numpy/numpy#8192
mj-will added a commit to mj-will/nessai that referenced this issue Jun 21, 2022
* Includes tests
* Needed because of this issue in numpy: numpy/numpy#8192
@seberg
Copy link
Member
seberg commented Jan 28, 2024

Same as gh-21539, closing this one.

@seberg seberg closed this as completed Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0