pandas.DataFrame.sparse.to_coo#

DataFrame.sparse.to_coo()[source]#

Return the contents of the frame as a sparse SciPy COO matrix.

Returns:
scipy.sparse.spmatrix

If the caller is heterogeneous and contains booleans or objects, the result will be of dtype=object. See Notes.

See also

DataFrame.sparse.to_dense

Convert a DataFrame with sparse values to dense.

Notes

The dtype will be the lowest-common-denominator type (implicit upcasting); that is to say if the dtypes (even of numeric types) are mixed, the one that accommodates all will be chosen.

e.g. If the dtypes are float16 and float32, dtype will be upcast to float32. By numpy.find_common_type convention, mixing int64 and and uint64 will result in a float64 dtype.

Examples

>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0, 1])})
>>> df.sparse.to_coo()
<COOrdinate sparse matrix of dtype 'int64'
    with 2 stored elements and shape (4, 1)>