numpy/int-mask-as-index
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | error | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
mask.astype(int) turns a mask into 0s and 1s, so indexing with it picks element 0 and element 1 over and over. Index with the mask itself.
large = y[(y > 15).astype(int)]large = y[y > 15]What the rule reports
Section titled “What the rule reports”The rule reports y[(y > 15).astype(int)], which picks element 0 and element 1 over and over. It also reports ~(y > 15).astype(int), which gives -1 and -2.
The rule leaves alone a lookup such as labels[(score > t).astype(int)], where the mask comes from another array. Such a lookup maps each value to one of two labels.
How to fix it
Section titled “How to fix it”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 y[y > 15]. The ~ form has no fix.
For the ~ form, decide whether the code meant to invert the mask or produce integers.
Diagnostic message
Section titled “Diagnostic message”$M.astype($T) turns the mask into 0s and 1s, so this index picks element 0 and element 1 over and over. Index with the mask itself, $X[$M].See all rules in the NumPy pack.