Skip to content

Commit

Permalink
BUG: sparse: Use _ascontainer for argmin/argmax (#18121)
Browse files Browse the repository at this point in the history
* BUG: sparse: Use _ascontainer for argmin/argmax

* Adding test coverage

* Remove unused import

* skip sparse formats that don't support min/max
  • Loading branch information
perimosocordiae committed Mar 27, 2023
1 parent ec4241c commit e799ae4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scipy/sparse/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np

from ._base import spmatrix, _ufuncs_with_fixed_point_at_zero
from ._sputils import isscalarlike, validateaxis, matrix
from ._sputils import isscalarlike, validateaxis

__all__ = []

Expand Down Expand Up @@ -250,7 +250,7 @@ def _arg_min_or_max_axis(self, axis, op, compare):
if axis == 1:
ret = ret.reshape(-1, 1)

return matrix(ret)
return self._ascontainer(ret)

def _arg_min_or_max(self, axis, out, op, compare):
if out is not None:
Expand Down
17 changes: 17 additions & 0 deletions scipy/sparse/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ def test_mean(A):
"Expected array, got matrix"


@parametrize_sparrays
def test_min_max(A):
# Some formats don't support min/max operations, so we skip those here.
if hasattr(A, 'min'):
assert not isinstance(A.min(axis=1), np.matrix), \
"Expected array, got matrix"
if hasattr(A, 'max'):
assert not isinstance(A.max(axis=1), np.matrix), \
"Expected array, got matrix"
if hasattr(A, 'argmin'):
assert not isinstance(A.argmin(axis=1), np.matrix), \
"Expected array, got matrix"
if hasattr(A, 'argmax'):
assert not isinstance(A.argmax(axis=1), np.matrix), \
"Expected array, got matrix"


@parametrize_sparrays
def test_todense(A):
assert not isinstance(A.todense(), np.matrix), \
Expand Down

0 comments on commit e799ae4

Please sign in to comment.