Inconsistency in indexing 0-d arrays with Ellipsis #3017

Closed
Gitusername opened this Issue Feb 25, 2013 · 2 comments

Projects

None yet

3 participants

@Gitusername

Attempting to set the element of a 0-d array by indexing it with (...,) causes an IndexError, but similar operations with Ellipsis work correctly:

>>> a = numpy.array(1)
>>> a[...]
array(1)
>>> a[(...,)]
array(1)
>>> a[...] = 4
>>> a
array(4)
>>> a[(...,)] = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: 0-d arrays can't be indexed.

I assume this should be allowed? It came up in some code along the following lines:

a = numpy.zeros(b.shape + c.shape)
for i in numpy.ndindex(c.shape):
    a[(...,) + i] = something

which fails to work when c is zero-dimensional.

@njsmith
NumPy member
@seberg
NumPy member

You probably got that already, but if you need a workaround, c[(slice(None),) * a.ndim + index] will work, since the empty tuple does not have this issue.

@dimasad dimasad added a commit to dimasad/numpy that referenced this issue Sep 22, 2013
@dimasad dimasad TST: added test for issue #3017.
Also changed style to please numpy developers.
094ebf7
@njsmith njsmith closed this in 61e0f2b Sep 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment