Skip to content

numpy/count-nonzero

Rule count-nonzero
Pack NumPy
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

Summing a comparison or taking the length of np.where counts matches the slow way. Call np.count_nonzero.

changed = np.sum(before != after)
changed = np.count_nonzero(before != after)

The rule reports np.sum(x > 0), len(np.where(c)[0]), len(np.nonzero(c)[0]), len(np.flatnonzero(c)), len(x[x > 0]) and np.sum(x > 2) > 0. In a file that imports pandas, the pandas rule len-of-filter reports len(x[x > 0]) instead, because x may be a frame or a series.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The safe fix writes np.any(x > 2). The unsafe fix writes np.count_nonzero(x > 0), also for len(x[x > 0]), which gave a Python int.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

Summing a comparison adds the True values one by one, which ran 1.7 times slower on 1,000,000 values. Write $NP.count_nonzero($C).

Browse the other rules of the NumPy pack.