pandas-dev/pandas · error · TypeError
Expected coo_matrix. Got
Error message
Expected coo_matrix. Got {type(A).__name__} instead. What it means
Thrown by coo_to_sparse_series in pandas/core/arrays/sparse/scipy_sparse.py:200 when the input lacks the .data/.row/.col attributes of a scipy.sparse.coo_matrix. The function wraps attribute access in try/except AttributeError and re-raises as TypeError, so any non-COO object (CSR, CSC, dense ndarray, list) is rejected with a clear message.
Solutions
- Convert to COO first: A = A.tocoo() before calling the pandas API.
- If starting from dense, build COO explicitly: scipy.sparse.coo_matrix(dense_arr).
- Type-check upstream: import scipy.sparse; if not scipy.sparse.isspmatrix_coo(A): A = A.tocoo().
Example fix
// before from scipy.sparse import csr_matrix A = csr_matrix((3,3)) coo_to_sparse_series(A) # raises TypeError // after coo_to_sparse_series(A.tocoo())
Defensive patterns
Strategy: type-guard
Validate before calling
import scipy.sparse
def ensure_coo(A):
if not scipy.sparse.isspmatrix_coo(A):
A = A.tocoo()
return A Type guard
import scipy.sparse
def is_coo(A) -> bool:
return scipy.sparse.isspmatrix_coo(A) Try / catch
try:
from pandas.core.arrays.sparse.scipy_sparse import coo_to_sparse_series
return coo_to_sparse_series(A)
except TypeError as e:
if 'Expected coo_matrix' in str(e):
return coo_to_sparse_series(A.tocoo())
raise Prevention
- After any scipy sparse arithmetic, call .tocoo() before handing the matrix back to pandas.
- Validate with scipy.sparse.isspmatrix_coo at the trust boundary.
- Document the COO requirement on functions that bridge scipy and pandas sparse.
When it happens
Trigger: Calling pd.DataFrame.sparse.from_coo(A) or the internal coo_to_sparse_series with A being a csr_matrix, csc_matrix, dense numpy array, list of lists, or a pandas DataFrame.
Common situations: User performs sparse matrix arithmetic which auto-converts COO to CSR/CSC, then feeds the result back to pandas without re-converting. Passing a dense matrix expecting implicit conversion.
Related errors
- Duplicate index entries are not allowed in to_coo…
- Is not a partition because intersection is not null.
- Is not a partition because union is not the whole.
- to_coo requires MultiIndex with nlevels >= 2.
- Column length mismatch
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/15cd2fb01d183d4d.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/sparse/scipy_sparse.py:200
Parameters
----------
A : scipy.sparse.coo_matrix
dense_index : bool, default False
Returns
-------
Series
Raises
------
TypeError if A is not a coo_matrix
"""
from pandas import SparseDtype
try:
ser = Series(A.data, MultiIndex.from_arrays((A.row, A.col)), copy=False)
except AttributeError as err:
raise TypeError(
f"Expected coo_matrix. Got {type(A).__name__} instead."
) from err
ser = ser.sort_index()
ser = ser.astype(SparseDtype(ser.dtype))
if dense_index:
ind = MultiIndex.from_product([A.row, A.col])
ser = ser.reindex(ind)
return ser
View on GitHub (pinned to 3b7651241d)