-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Fix array_equal and array_equiv issue #3322
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
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.
@@ -2179,7 +2179,7 @@ def array_equiv(a1, a2): | |||
except: | |||
return False | |||
try: | |||
return bool(equal(a1,a2).all()) | |||
return bool(asarray(a1 == a2).all()) |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.