Skip to content

numpy/where-tuple-length

Pack Severity Fix Versions
NumPy error unsafe, with xpd check --fix --unsafe-fixes All versions

np.where with one argument returns a tuple with one array per dimension, so its length counts dimensions and not matches. Use np.count_nonzero.

matches = len(np.where(z > 0))
matches = np.count_nonzero(z > 0)

The rule reports len(np.where(z > 0)), len(np.nonzero(z)) and if np.where(z > 9):, because np.where with one argument returns a tuple with one array per dimension. The length of the tuple is the number of dimensions, and the tuple is always true.

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 unsafe fix writes np.count_nonzero(z > 0).

$F returns a tuple with one array per dimension, so its length is the number of dimensions and not the number of matches. Write $NP.count_nonzero($C).

See all rules in the NumPy pack.