Skip to content

scipy/empty-csr-write

Pack Severity Fix Versions
SciPy warning unsafe, with xpd check --fix --unsafe-fixes All versions

Writing into a CSR or CSC matrix that started empty warns, raises under python -W error, and copies the whole matrix each time. Build it as lil_array, dok_array or from coordinates, then convert once.

A = sparse.csr_matrix((n, n))
A = sparse.lil_array((n, n))
for i, j, v in triples:
A[i, j] = v
A = A.tocsr()

The rule reports a write into a csr_matrix, csc_matrix, csr_array or csc_array built from a shape alone, as in A = csr_matrix((n, m)) followed by A[i, j] = v, A[i, j] += v or a slice write, in a function that doesn't assign A again. Each write adds a stored value and copies the whole matrix. On SciPy 1.18.1 a write emits SparseEfficiencyWarning, which raises under python -W error. Moreover, 1,000 writes ran 30 times slower than the same writes into a lil_matrix.

The finding points to the constructor. A shape supplied as X.shape can match, while constructors given stored values and index arrays are left alone.

xpd check --fix --unsafe-fixes rewrites module-qualified empty csr_matrix/csc_matrix constructors to lil_matrix and csr_array/csc_array constructors to lil_array. Bare imported names remain manual, and you can convert back with .tocsr() or .tocsc() once writes finish.

$A starts as an empty $N, and a later write into it adds a stored value, which emits SparseEfficiencyWarning, raises under python -W error, and copies the whole matrix each time. Build it as lil_array or dok_array, or collect the coordinates and pass them to coo_array, and call tocsr or tocsc once at the end.

See all rules in the SciPy pack.