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 2 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 :class:`SparseAccessor` so user knows when they provide the incorrect matrix format to `from_coo` (:issue:`26554`)

Other
^^^^^
Expand Down
12 changes: 8 additions & 4 deletions pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2014,11 +2014,15 @@ def from_coo(cls, A, dense_index=False):
from pandas.core.sparse.scipy_sparse import _coo_to_sparse_series
from pandas import Series

result = _coo_to_sparse_series(A, dense_index=dense_index,
sparse_series=False)
result = Series(result.array, index=result.index, copy=False)
try:
result = _coo_to_sparse_series(A, dense_index=dense_index,
sparse_series=False)
result = Series(result.array, index=result.index, copy=False)

return result
return result

except AttributeError:
raise AttributeError('Expected coo matrix format.')

def to_coo(self, row_levels=(0, ), column_levels=(1, ), sort_labels=False):
"""
Expand Down
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(AttributeError):
pd.Series.sparse.from_coo(m)
7 changes: 7 additions & 0 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,13 @@ def test_from_coo(self):
expected = pd.Series([4, 9, 7, 5], index=index, dtype='Sparse[int]')
tm.assert_series_equal(result, expected)

@td.skip_if_no_scipy
def test_from_coo_invalid_input(self):
import scipy.sparse
m = scipy.sparse.csr_matrix(np.array([[0, 1], [0, 0]]))
with pytest.raises(AttributeError):
pd.Series.sparse.from_coo(m)

@td.skip_if_no_scipy
def test_to_coo(self):
import scipy.sparse
Expand Down