Repository navigation
Fix array_equal and array_equiv issue - #3322
Conversation
array_equal() and array_equiv() don't work for string arrays and record arrays. This is because those methods use numpy.equals ufunc for comparison, and there are no equal ufuncs registered for strings and record arrays. Replace numpy.equals with '==' array operation, which handles strings and record arrays.
There was a problem hiding this comment.
Is it OK to have asarray applied after the the == operator rather than applying it to the operands?
There was a problem hiding this comment.
Yeah, but the asarray can be just removed here, since in both functions a1 and a2 are already converted to arrays before this line.
There was a problem hiding this comment.
@seberg If the two operands are 0d arrays, the == operator will return a bool object instead of an array, in which case the call to all() will fail. I could add a special case for this though, if unnecessarily calling asarray() is too costly.
Just realized I need to handle this properly in array_equal() too.
There was a problem hiding this comment.
Ah, right. The performance of asarray is no worry to me (it just returns immediately since it is already an array anyway). It might be that all works on the np.bool_ object, but I don't care either way then.
There was a problem hiding this comment.
I added asarray call to array_equal too. My reasoning that I stated earlier wasn't quite correct. The == operator actually returns a np.bool_ object for 0d arrays, but returns a False object for two arrays that don't broadcast.
|
This should also close #146. |
Fix array_equal and array_equiv issue
|
@jayvius Could you backport this to 1.7.x? |
|
Is there a problem? |
|
Ok great. I'm not sure if I totally understand if the issue being discussed here is related to the following. But, with numpy version 1.16, I try to run the following and it fails. Do we have an open issue to support it or is it already fixed in later versions? Thanks in advance. |
|
It seems we have one here, thanks in advance. |
array_equal() and array_equiv() don't work for string arrays and record arrays. This is because those methods use numpy.equals ufunc for comparison, and there are no equal ufuncs registered for strings and record arrays. Replace numpy.equals with '==' array operation, which handles strings and record arrays. Fixes #3015. Closes #146.