Allow use of ellipsis in scipy.sparse slicing #2721

jnothman opened this Issue Aug 13, 2013 · 4 comments

3 participants


It should be legal to use an Ellipsis to slice sparse matrices so that they may be used alongside ndarrays.

>>> import numpy as np
>>> m = np.matrix([[0,1], [2,3]])
>>> import scipy.sparse as sp
>>> s = sp.csr_matrix(m)
>>> m[..., 1]
>>> s[..., 1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/", line 249, in __getitem__
    P = extractor(row, self.shape[0])        #[[1,2],j] or [[1,2],1:2]
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/", line 209, in extractor
    indices = asindices(indices)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/", line 191, in asindices
    raise IndexError('invalid index')
IndexError: invalid index

There is currently a PR here for fancy indexing in Sparse matrices. I'll look into it asap.


So... Since sparse matrices are limited to 2D, the Ellipsis is exactly the same as a :?


Yes. It's an uninteresting case, but compatibility is required.

SciPy member
pv commented Aug 13, 2013

Same as :, except that x[:,:,...] and x[:,...,:], x[...,:,:] are also allowed for 2D

@pv pv closed this in 9f19af5 Sep 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment