Skip to content

Fix array_equal and array_equiv issue - #3322

Merged
charris merged 2 commits into
numpy:masterfrom
ContinuumIO:array_equal_fix
May 13, 2013
Merged

charris merged 2 commits into
numpy:masterfrom
ContinuumIO:array_equal_fix

Conversation

@jayvius

@jayvius jayvius commented May 10, 2013

Copy link
Copy Markdown
Contributor

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.
Comment thread numpy/core/numeric.py

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

charris commented May 11, 2013

Copy link
Copy Markdown
Member

This should also close #146.

@charris

charris commented May 13, 2013

Copy link
Copy Markdown
Member

@jayvius Could you backport this to 1.7.x?

@mohtamohit

Copy link
Copy Markdown

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

mattip commented Mar 26, 2021

Copy link
Copy Markdown
Member

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
Copy Markdown

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
Copy Markdown

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