Skip to content

ENH: Add keepdims argument to count_nonzero#15870

Merged
seberg merged 5 commits intonumpy:masterfrom
WarrenWeckesser:count-nonzero-keepdims
Mar 31, 2020
Merged

ENH: Add keepdims argument to count_nonzero#15870
seberg merged 5 commits intonumpy:masterfrom
WarrenWeckesser:count-nonzero-keepdims

Conversation

@WarrenWeckesser
Copy link
Member

A simple enhancement to count_nonzero:

In [3]: a
Out[3]: 
array([[ 0,  1,  7,  0],
       [ 3,  0,  2, 19]])

In [4]: np.count_nonzero(a, axis=1, keepdims=True)
Out[4]: 
array([[2],
       [3]])

@WarrenWeckesser
Copy link
Member Author

The TravisCI test failure on the s390x platform looks unrelated to this PR.

@WarrenWeckesser
Copy link
Member Author

Reran the tests and all passed--looks like the TravisCI test failure on the s390x platform was a transient.

@eric-wieser
Copy link
Member

Needs a release note, but this looks like an obviously sane addition.

@WarrenWeckesser
Copy link
Member Author

Thanks @eric-wieser, I added a release note.

@seberg seberg self-requested a review March 31, 2020 14:41
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

LGTM, just the None path needs fixing. Nice generalization, I agree that this is a reduce-like operation and thus should have the keepdims argument. kwarg-only is a nice touch.



def _count_nonzero_dispatcher(a, axis=None):
def _count_nonzero_dispatcher(a, axis=None, *, keepdims=False):
Copy link
Member

Choose a reason for hiding this comment

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

@shoyer shouldn't this be always None for the dispatching signature, did it actually matter?

Copy link
Member

Choose a reason for hiding this comment

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

I think it was just convention to avoid defaults getting out of sync. Still worthwhile though.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I changed the keepdims default in the dispatcher function signature to None.

Copy link
Member

@shoyer shoyer Mar 31, 2020

Choose a reason for hiding this comment

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

Hmm, it looks like our import-time check to verify default values of dispatcher arguments may be broken on keyword only argument. This should have caused a test failure.

array([[2],
[3]])
"""
if axis is None:
Copy link
Member

Choose a reason for hiding this comment

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

This seems incorrect, it has to be axis is None and not keepdims. Also needs a test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, fixed.

@seberg
Copy link
Member

seberg commented Mar 31, 2020

Thanks, lets put this in!

@seberg seberg merged commit 170d1bb into numpy:master Mar 31, 2020
@WarrenWeckesser WarrenWeckesser added this to the 1.19.0 release milestone Mar 31, 2020
@WarrenWeckesser WarrenWeckesser deleted the count-nonzero-keepdims branch April 23, 2020 20:18
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.

4 participants