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

Skip to content

API: Change string to bool conversions to be consistent with Python #23898

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 1 commit into from
Jun 9, 2023

Conversation

seberg
Copy link
Member

@seberg seberg commented Jun 8, 2023

This changes casts from strings to bool to use the length (and not go via integers).
Further, nonzero behavior was taking into account spaces previously.

There is for users to silently always getting False now if they used the integer conversion logic (making their code wrong).


Most parsing functions are not affected by this, but manually parsing can be and so is np.fromregex unfortunately.

Closes gh-9875

This changes casts from strings to bool to use the length (and not
go via integers).
Further, `nonzero` behavior was taking into account spaces previously.

There is for users to silently always getting `False`
now if they used the integer conversion logic (making their code
wrong).
@seberg seberg added the 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified. label Jun 8, 2023
@ngoldbaum
Copy link
Member

For what it's worth, this is also how stringdtype currently works:

>>> np.array(['', ' ', '0', '\t'], dtype=StringDType()).astype(bool)
array([False,  True,  True,  True])
>>> np.nonzero(np.array(['', ' ', '0', '\t'], dtype=StringDType()))
(array([1, 2, 3]),)

@seberg
Copy link
Member Author

seberg commented Jun 9, 2023

I will consider this a 👍, I think @stefanv was also in favor?

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly makes sense to me (and code looks good)

@stefanv
Copy link
Contributor

stefanv commented Jun 9, 2023

Yes, I appreciate the consistency with Python.

@charris charris merged commit 19f86c3 into numpy:main Jun 9, 2023
@charris
Copy link
Member

charris commented Jun 9, 2023

Thanks Sebastian.

@seberg seberg deleted the string-nonzero-sanity branch June 10, 2023 06:23
seberg added a commit to seberg/numpy that referenced this pull request Jan 22, 2024
I didn't allow them because casting strings to bools made no sense
at all.  But that was fixed in numpygh-23898.

I am very sure the deprecation is expired and I simply forgot to
remove that branch also.

Closes numpygh-1912
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
30 - API 62 - Python API Changes or additions to the Python API. Mailing list should usually be notified.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

BUG: truthiness of strings is arbitrary, context-dependent, and inconsistent
5 participants