numpy/write-to-copy
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | error | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Indexing with a mask or a list of positions gives a copy, so writing into that result never reaches the array. Index the array once with the selections combined.
x[x > 2][0] = -1x[np.flatnonzero(x > 2)[0]] = -1What the rule reports
Section titled “What the rule reports”The rule reports x[x > 2][0] = v, x[[0, 1]][0] = v, m.flatten()[0] = v and x.astype(int)[0] = v. Each one writes to a copy, so the array keeps its old values.
The rule leaves x[1:4][0] = v and m[0][mask] = v alone, because indexing with a slice or a whole number returns a view.
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 x[0] = 9 for x.astype(int)[0] = 9 and x.copy()[0] = 9, so the write reaches x, cast to the dtype of x. A mask or a list of positions, and flatten, have no fix.
flatten also has no fix because the second index counts flattened values, while indexing the original array can select a row.
Diagnostic message
Section titled “Diagnostic message”$X[$A] is a copy, because the index is a mask or a list of positions, so this write never reaches $X. Index $X once, with the two selections combined.See all rules in the NumPy pack.