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

Skip to content

isin() is not matching Boolean values correctly #11999

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

Open
tumido opened this issue Sep 20, 2018 · 4 comments
Open

isin() is not matching Boolean values correctly #11999

tumido opened this issue Sep 20, 2018 · 4 comments
Labels

Comments

@tumido
Copy link

tumido commented Sep 20, 2018

Using np.isin() to match boolean values results in a strange, unexpected, behavior.

Reproducing code example:

>>> import numpy as np
>>> arr = np.array([True, np.bool_(True), "True", None])
>>> np.isin(arr, [True])
array([ True,  True, False, False])  # Correct
>>> np.isin(arr, [np.bool_(True)])
array([ True,  True, False, False])  # Correct
>>> np.isin(arr, ['True'])
array([False, False,  True, False])  # Correct
>>> np.isin(arr, [True, 'True'])
array([False, False,  True, False])  # Wrong
>>> np.isin(arr, [np.bool_(True), True, 'True'])
array([False, False,  True, False])  # Wrong
>>> np.isin(arr, [True, 'True', None])
array([ True,  True,  True,  True])  # Correct
>>> np.isin(arr, ['True', None])
array([False, False,  True,  True])  # Correct

For False values it behaves the same (bad) as for True

Numpy/Python version information:

>>> import sys, numpy; print(numpy.__version__, sys.version)
1.15.1 3.6.6 (default, Sep 13 2018, 16:29:18)
[GCC 8.2.1 20180831]
@Ladas
Copy link

Ladas commented Sep 21, 2018

a problem here is in casting

>>> print(np.array([True, 'True']))
['True' 'True'] # dtype is <U4, in other cases it's casted as object

so you'd have to explicitely set the type to

>>> print(np.isin(arr, np.array([True, 'True'], dtype=object)))
[ True  True  True False] # correct

@tumido
Copy link
Author

tumido commented Sep 21, 2018

I agree. The isin() is not verifying/providing the cast properly. It allows an array_like to be passed as test_elements. It does the conversion to a np.array itself, but it's not ensuring the dtype is the same on both structures (elements and test_elements) or something like that...

@shoyer
Copy link
Member

shoyer commented Sep 24, 2018

See #6550 for a discussion of the larger issue with NumPy's casting rules promoting mixed string/anything to string.

@shoyer
Copy link
Member

shoyer commented Sep 24, 2018

This is actually on our roadmap under "better coercion for string + number"

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

No branches or pull requests

4 participants