{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/sparse/scipy_sparse.py#L182-L209","documentation":"Raised by coo_to_sparse_series when the input lacks the .data/.row/.col attributes of a scipy.sparse.coo_matrix. The function wraps the attribute access in a try/except AttributeError and re-raises as TypeError so callers get a clear contract violation instead of a confusing AttributeError. Only scipy.sparse.coo_matrix is accepted because the conversion logic reads A.data, A.row, and A.col directly.","triggerScenarios":"Calling pandas.core.arrays.sparse.scipy_sparse.coo_to_sparse_series with a csr_matrix, csc_matrix, lil_matrix, dok_matrix, a dense numpy.ndarray, a list, or any object that is not a scipy.sparse.coo_matrix. Also triggered indirectly through SparseDtype round-trips that pass the wrong sparse format.","commonSituations":"User obtains a csr_matrix from scikit-learn or scipy and tries to convert it to a pandas SparseSeries without converting format first. Copy-pasting code that worked on coo output but now feeds in a different sparse format.","solutions":["Convert the matrix to coo format before calling: coo_to_sparse_series(A.tocoo()).","Check the format up front: if A.format != 'coo': A = A.tocoo().","Use scipy.sparse.coo_matrix directly when constructing data destined for pandas sparse Series."],"exampleFix":"// before\nfrom pandas.core.arrays.sparse.scipy_sparse import coo_to_sparse_series\nseries = coo_to_sparse_series(csr_mat)\n\n// after\nseries = coo_to_sparse_series(csr_mat.tocoo())","handlingStrategy":"validation","validationCode":"import scipy.sparse\n\ndef to_sparse_series(A):\n    if not (scipy.sparse.issparse(A) and A.format == 'coo'):\n        A = A.tocoo()\n    return coo_to_sparse_series(A)","typeGuard":"import scipy.sparse\n\ndef is_coo_matrix(A) -> bool:\n    return scipy.sparse.issparse(A) and getattr(A, 'format', None) == 'coo'","tryCatchPattern":null,"preventionTips":["Always call .tocoo() on sparse matrices before passing to coo_to_sparse_series.","Check A.format == 'coo' at trust boundaries in generic conversion utilities.","Document the coo_matrix requirement in wrapper functions."],"tags":["scipy","sparse","type-error","conversion"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}