Skip to content

DOC: Add note to nonzero docstring. - #13551

Merged
mattip merged 12 commits into
numpy:masterfrom
abhinavsagar:addednote
May 29, 2019
Merged

mattip merged 12 commits into
numpy:masterfrom
abhinavsagar:addednote

Conversation

@abhinavsagar

@abhinavsagar abhinavsagar commented May 13, 2019 •

Copy link
Copy Markdown
Contributor

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

Fixes #13522

Comment thread numpy/core/fromnumeric.py Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Comment thread numpy/core/fromnumeric.py Outdated
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
Copy Markdown
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
Copy Markdown
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
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 Done the required changes.

@umanwizard

Copy link
Copy Markdown

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.

Comment thread numpy/core/fromnumeric.py Outdated
row-major, C-style order. The corresponding non-zero
values can be obtained with::

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.

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

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.

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

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

Comment thread numpy/core/fromnumeric.py Outdated
-----
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

mattip commented May 29, 2019

Copy link
Copy Markdown
Member

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