Skip to content

Add more specific error message when user passes incorrect matrix format to from_coo #26584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,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 more specific error message in :meth:`_coo_to_sparse_series` so it returns a TypeError for inputs that are not coo matrices (:issue:`26554`)

Other
^^^^^
Expand Down
40 changes: 23 additions & 17 deletions pandas/core/sparse/scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,29 @@ def _coo_to_sparse_series(A, dense_index: bool = False,

Returns
-------
Series or SparseSeries
Series or SparseSeries on success
TypeError if A is not a coo_matrix

"""
from pandas import SparseDtype

s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
s = s.sort_index()
if sparse_series:
# TODO(SparseSeries): remove this and the sparse_series keyword.
# This is just here to avoid a DeprecationWarning when
# _coo_to_sparse_series is called via Series.sparse.from_coo
s = s.to_sparse() # TODO: specify kind?
else:
s = s.astype(SparseDtype(s.dtype))
if dense_index:
# is there a better constructor method to use here?
i = range(A.shape[0])
j = range(A.shape[1])
ind = MultiIndex.from_product([i, j])
s = s.reindex(ind)
return s
try:
s = Series(A.data, MultiIndex.from_arrays((A.row, A.col)))
s = s.sort_index()
if sparse_series:
# TODO(SparseSeries): remove this and the sparse_series keyword.
# This is just here to avoid a DeprecationWarning when
# _coo_to_sparse_series is called via Series.sparse.from_coo
s = s.to_sparse() # TODO: specify kind?
else:
s = s.astype(SparseDtype(s.dtype))
if dense_index:
# is there a better constructor method to use here?
i = range(A.shape[0])
j = range(A.shape[1])
ind = MultiIndex.from_product([i, j])
s = s.reindex(ind)
return s
except AttributeError:
raise TypeError('Expected coo_matrix. Got {} instead.'
.format(type(A).__name__))
7 changes: 7 additions & 0 deletions pandas/tests/arrays/sparse/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,10 @@ 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(self):
import scipy.sparse
m = scipy.sparse.csr_matrix(np.array([[0, 1], [0, 0]]))
with pytest.raises(TypeError):
pd.Series.sparse.from_coo(m)