Skip to content

DOC: Add note to nonzero docstring. #13551

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 12 commits into from
May 29, 2019
Merged

DOC: Add note to nonzero docstring. #13551

merged 12 commits into from
May 29, 2019

Conversation

abhinavsagar
Copy link
Contributor

@abhinavsagar abhinavsagar commented May 13, 2019

Document problems with using x.nonzero for indexing, i.e., never use x[y.nonzero()], use x[y.astype(bool)] instead.

Fixes #13522

@@ -1761,6 +1761,8 @@ def nonzero(a):
"""
Return the indices of the elements that are non-zero.

NOTE - In place of using x[y.nonzero()], it is recommended to use x[y.astype(bool)].

Copy link
Member

Choose a reason for hiding this comment

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

Please move this to after the Return section as

Notes
-----
To obtain the non-zero values, it is recommended to use ``x[y.astype(bool)]`` which
will correctly handle 0-d arrays.

and refactor the paragraph at 1766 to better express the idea that nonzero is the wrong tool for directly indexing the array.
If a line goes over 80 characters, break it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mattip Ping

Copy link
Member

Choose a reason for hiding this comment

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

the text "The corresponding non-zero ..." on line 1767 above needs rephrasing or removal.

@charris charris changed the title Added NOTE in numpy\core\fromnumeric.py DOC: Add note to nonzero docstring. May 13, 2019
Notes
-----
To obtain the non-zero values, it is recommended to use ``x[y.astype(bool)]``
which will correctly handle 0-d arrays.
Copy link
Member

Choose a reason for hiding this comment

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

The description you use here makes x vs y look like a typo. It would be good to include the form you're recommending against here too.

Also, worth commenting that in most cases y is already a boolean array, so as type is unnecessary

Copy link
Member

Choose a reason for hiding this comment

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

@abhinavsagar could you follow up on this, and the other comments? I think it would be good to keep the old comment about the values and move it here, as well as mentioning the astype is not necessary.

The story would be that you can get the values using the old statement (with the exception of 0d), but if you only need the values, you should prefer the other statement.

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 Done the required changes.

@umanwizard
Copy link

I'm not a member of the NumPy project, so feel free to take my review with a grain of salt if you like. But I am the one who opened issue #13522 which you say this fixes.

I think (in addition to the note you've added here), it should be mentioned that a.nonzero() returns a tuple of length 1 when a has zero dimensions -- that was the main point of the issue I opened.

values can be obtained with::

Copy link
Member

Choose a reason for hiding this comment

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

Nit: let's not add random whitespace to the patch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I was changing from my phone.

@seberg seberg assigned seberg and unassigned seberg May 23, 2019
@seberg seberg self-requested a review May 23, 2019 18:32
@eric-wieser
Copy link
Member

returns a tuple of length 1 when a has zero dimensions -- that was the main point of the issue I opened.

Given we're leaning towards a deperecation there, it might not be worth documenting

@abhinavsagar
Copy link
Contributor Author

@eric-wieser Done. Is it ready to be merged now ?

-----
To obtain the non-zero values, it is recommended to use ``x[y.astype(bool)]``
which will correctly handle 0-d arrays. In most cases y is already a boolean
array, so astype is not necessary.
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be x[x.astype(bool)] without the y?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

Now the y in the second line is wrong. Also since we now are using x, the sentence beginning "In most cases ..." since we don't know anything about x. Probably should drop that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. It seems like we came back from where we started.

@seberg seberg removed their request for review May 28, 2019 17:34
@mattip mattip merged commit 1f42735 into numpy:master May 29, 2019
@mattip
Copy link
Member

mattip commented May 29, 2019

Thanks @abhinavsagar

@abhinavsagar abhinavsagar deleted the addednote branch May 29, 2019 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs are unclear about behavior of nonzero on zero-dim arrays.
6 participants