ENH: make array_equal+array_equiv work for record arrays/structured arrays #146

wants to merge 2 commits into


None yet

3 participants


Implementation of http://projects.scipy.org/numpy/ticket/1941 with tests. Please comment if the behaviour of array_equiv in commit 4b896f8 or in HEAD is preferred.

Derek Homeier added some commits Aug 26, 2011
Derek Homeier ENH: enable comparison of structured arrays with array_equal|equiv 4b896f8
Derek Homeier Extended the definition of array_equiv to different field names
Modified array_equiv to consider structured arrays with differently
named fields, but equivalent data, to be equivalent.
mwiebe commented on c036bce Aug 30, 2011

I'd much prefer for the names to match exactly, matching the way NumPy already does struct array comparison:

>>> x = np.array([(0,1),(2,3)], dtype=[('a', 'i4'), ('b', 'i4')])
>>> y = np.array([(0,1),(2,3)], dtype=[('a', 'i4'), ('b', 'i4')])
>>> x == y
array([ True,  True], dtype=bool)
>>> y = np.array([(0,1),(2,4)], dtype=[('a', 'i4'), ('b', 'i4')])
>>> x == y
array([ True, False], dtype=bool)
>>> y = np.array([(0,1),(2,3)], dtype=[('a', 'i4'), ('c', 'i4')])
>>> x == y

That would make the difference between array_equal and array_equiv - but structure array comparison seems to behave similar to both, so maybe it's really a matter of taste:

>>> x = np.array([(0,1),(2,3)], dtype=[('a', 'i4'), ('b', 'i4')])
>>> y = np.array([(0,1),(2,3)], dtype=[('a', 'i4'), ('b', 'i4')]).reshape(1,-1)
>>> x == y
array([[ True,  True]], dtype=bool)

I was not aware that '==' did work already, so maybe most of the array_equal code could simply be replaced by

return bool((a1 == a2).all())


mwiebe commented Sep 1, 2011

I actually fixed the == in 1.6. It had already been implemented, but incorrectly. I think even just

return (a1 == a2).all()

should be fine, but it's worth having tests which confirm this. One thing to think about for the future, is that I think == for struct dtypes should return a struct dtype with matching names but all 'bool' dtypes, and all() should be extended to reduce the bool-struct to a single value as well... The above line of code would still work correctly with these changes, so that's good.

charris commented May 3, 2013

@dhomeier How does this story end?

charris commented May 13, 2013

Closed by #3322.

@charris charris closed this May 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment