Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
May 13, 2013

Conversation

jayvius
Copy link
Contributor

@jayvius jayvius commented May 10, 2013

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.

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())
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

@charris
Copy link
Member

charris commented May 11, 2013

This should also close #146.

@charris
Copy link
Member

charris commented May 13, 2013

@jayvius Could you backport this to 1.7.x?

@mohtamohit
Copy link

Hey @charris @jayvius, is there a way I can see that this support for strings for np.equal was rolled out in which version of numpy?

@mattip
Copy link
Member

mattip commented Mar 26, 2021

git tag --contains 6dc709a5aa0b17b5a74b7456d7cbd2580d9063f6 says this PR is in v1.8.0. The comment above mentions backporting to 1.7.x, maybe it was done there too.

Is there a problem?

@mohtamohit
Copy link

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?

In [23]: y = np.array(['abc', 'pqr'])
In [24]: y.dtype.type
Out[24]: numpy.str_
In [25]: np.equal(y, y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-da698f6c7142> in <module>
----> 1 np.equal(y, y)
TypeError: ufunc 'equal' did not contain a loop with signature matching types dtype('<U3') dtype('<U3') dtype('bool')

Thanks in advance.

@mohtamohit
Copy link

It seems we have one here, thanks in advance.

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

Successfully merging this pull request may close these issues.

array_equal fails on string dtypes
5 participants