[Numpy-discussion] Rethinking multiple dimensional indexing with sequences?

Sebastian Berg sebastian at sipsolutions.net
Tue Feb 18 11:24:47 EST 2014


On Di, 2014-02-18 at 17:09 +0100, Sebastian Berg wrote:
> Hey all,
> 
<snip>
> Now also NumPy commonly uses lists here to build up indexing tuples
> (since they are mutable), however would it really be so bad if we had to
> do `arr[tuple(slice_list)]` in the end to resolve this issue? So the
> proposal would be to deprecate anything but (base class) tuples, or
> maybe at least only allow this weird logic for lists and not all
> sequences. I do not believe we can find a logic to decide what to do
> which will not be broken in some way...
> 

You may wonder why I would suddenly care. The reason is that
array-like's such as pandas types should behave like arrays in indexing.
With this logic in place for arbitrary sequences, these might (even
suddenly!) switch to the nd-index behaviour.

> PS: The code implementing the "advanced index or nd-index" logic is
> here:
> https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/mapping.c#L196
> 
> - Sebastian
> 
> 
> Another confusing example:
> 
> In [9]: a = np.arange(10)
> 
> In [10]: a[[(0, 1), (2, 3)] * 17] # a[np.array([(0, 1), (2, 3)] * 17)]
> Out[10]: 
> array([[0, 1],
>       <snip>
>        [2, 3]])
> 
> In [11]: a[[(0, 1), (2, 3)]] # a[np.array([0, 1]), np.array([2, 3])]
> ---------------------------------------------------------------------------
> IndexError                                Traceback (most recent call
> last)
> <ipython-input-11-57b93f64dfa6> in <module>()
> ----> 1 a[[(0, 1), (2, 3)]]
> 
> IndexError: too many indices for array
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 





More information about the NumPy-Discussion mailing list