Skip to content

MAINT: Fix all special-casing of dtypes in count_nonzero #9849

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
Oct 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,33 +411,13 @@ def count_nonzero(a, axis=None):

a = asanyarray(a)

if a.dtype == bool:
return a.sum(axis=axis, dtype=np.intp)

if issubdtype(a.dtype, np.number):
return (a != 0).sum(axis=axis, dtype=np.intp)

if issubdtype(a.dtype, np.character):
nullstr = a.dtype.type('')
return (a != nullstr).sum(axis=axis, dtype=np.intp)

axis = asarray(normalize_axis_tuple(axis, a.ndim))
counts = np.apply_along_axis(multiarray.count_nonzero, axis[0], a)

if axis.size == 1:
return counts.astype(np.intp, copy=False)
# TODO: this works around .astype(bool) not working properly (gh-9847)
if np.issubdtype(a.dtype, np.character):
a_bool = a != a.dtype.type()
else:
# for subsequent axis numbers, that number decreases
# by one in this new 'counts' array if it was larger
# than the first axis upon which 'count_nonzero' was
# applied but remains unchanged if that number was
# smaller than that first axis
#
# this trick enables us to perform counts on object-like
# elements across multiple axes very quickly because integer
# addition is very well optimized
return counts.sum(axis=tuple(axis[1:] - (
axis[1:] > axis[0])), dtype=np.intp)
a_bool = a.astype(np.bool_, copy=False)

return a_bool.sum(axis=axis, dtype=np.intp)


def asarray(a, dtype=None, order=None):
Expand Down