{"record":{"id":"15cd2fb01d183d4d","repo":"pandas-dev/pandas","slug":"expected-coo-matrix-got-type-a-name-instea","errorCode":null,"errorMessage":"Expected coo_matrix. Got {type(A).__name__} instead.","messagePattern":"Expected coo_matrix\\. Got (.+?) instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/sparse/scipy_sparse.py","lineNumber":200,"sourceCode":"    Parameters\n    ----------\n    A : scipy.sparse.coo_matrix\n    dense_index : bool, default False\n\n    Returns\n    -------\n    Series\n\n    Raises\n    ------\n    TypeError if A is not a coo_matrix\n    \"\"\"\n    from pandas import SparseDtype\n\n    try:\n        ser = Series(A.data, MultiIndex.from_arrays((A.row, A.col)), copy=False)\n    except AttributeError as err:\n        raise TypeError(\n            f\"Expected coo_matrix. Got {type(A).__name__} instead.\"\n        ) from err\n    ser = ser.sort_index()\n    ser = ser.astype(SparseDtype(ser.dtype))\n    if dense_index:\n        ind = MultiIndex.from_product([A.row, A.col])\n        ser = ser.reindex(ind)\n    return ser\n","sourceCodeStart":182,"sourceCodeEnd":209,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/sparse/scipy_sparse.py#L182-L209","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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()."],"exampleFix":"// before\nfrom scipy.sparse import csr_matrix\nA = csr_matrix((3,3))\ncoo_to_sparse_series(A)  # raises TypeError\n\n// after\ncoo_to_sparse_series(A.tocoo())","handlingStrategy":"type-guard","validationCode":"import scipy.sparse\ndef ensure_coo(A):\n    if not scipy.sparse.isspmatrix_coo(A):\n        A = A.tocoo()\n    return A","typeGuard":"import scipy.sparse\ndef is_coo(A) -> bool:\n    return scipy.sparse.isspmatrix_coo(A)","tryCatchPattern":"try:\n    from pandas.core.arrays.sparse.scipy_sparse import coo_to_sparse_series\n    return coo_to_sparse_series(A)\nexcept TypeError as e:\n    if 'Expected coo_matrix' in str(e):\n        return coo_to_sparse_series(A.tocoo())\n    raise","preventionTips":["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."],"tags":["sparse","scipy","coo","type-check"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}