diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 1619ba1a45739..95b7b3dce82da 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -692,7 +692,7 @@ Sparse - Significant speedup in :class:`SparseArray` initialization that benefits most operations, fixing performance regression introduced in v0.20.0 (:issue:`24985`) - Bug in :class:`SparseFrame` constructor where passing ``None`` as the data would cause ``default_fill_value`` to be ignored (:issue:`16807`) - Bug in :class:`SparseDataFrame` when adding a column in which the length of values does not match length of index, ``AssertionError`` is raised instead of raising ``ValueError`` (:issue:`25484`) - +- Introduce a better error message in :meth:`Series.sparse.from_coo` so it returns a ``TypeError`` for inputs that are not coo matrices (:issue:`26554`) Other ^^^^^ diff --git a/pandas/core/sparse/scipy_sparse.py b/pandas/core/sparse/scipy_sparse.py index 7630983421ff9..0dd8958e93c13 100644 --- a/pandas/core/sparse/scipy_sparse.py +++ b/pandas/core/sparse/scipy_sparse.py @@ -130,10 +130,19 @@ def _coo_to_sparse_series(A, dense_index: bool = False, Returns ------- Series or SparseSeries + + Raises + ------ + TypeError if A is not a coo_matrix + """ from pandas import SparseDtype - s = Series(A.data, MultiIndex.from_arrays((A.row, A.col))) + try: + s = Series(A.data, MultiIndex.from_arrays((A.row, A.col))) + except AttributeError: + raise TypeError('Expected coo_matrix. Got {} instead.' + .format(type(A).__name__)) s = s.sort_index() if sparse_series: # TODO(SparseSeries): remove this and the sparse_series keyword. diff --git a/pandas/tests/arrays/sparse/test_accessor.py b/pandas/tests/arrays/sparse/test_accessor.py index 370d222c1ab4e..d0a188a8aff3c 100644 --- a/pandas/tests/arrays/sparse/test_accessor.py +++ b/pandas/tests/arrays/sparse/test_accessor.py @@ -119,3 +119,13 @@ def test_series_from_coo(self, dtype, dense_index): ) tm.assert_series_equal(result, expected) + + @td.skip_if_no_scipy + def test_series_from_coo_incorrect_format_raises(self): + # gh-26554 + import scipy.sparse + m = scipy.sparse.csr_matrix(np.array([[0, 1], [0, 0]])) + with pytest.raises(TypeError, + match='Expected coo_matrix. Got csr_matrix instead.' + ): + pd.Series.sparse.from_coo(m)