[NumPy-Tickets] [NumPy] #402: newaxis incompatible with array indexing

NumPy Trac numpy-tickets@scipy....
Wed Mar 23 16:57:33 CDT 2011

```#402: newaxis incompatible with array indexing
---------------------------+------------------------------------------------
Reporter:  NeilenMarais  |       Owner:  somebody
Type:  enhancement   |      Status:  closed
Priority:  normal        |   Milestone:  Unscheduled
Component:  numpy.core    |     Version:
Resolution:  wontfix       |    Keywords:
---------------------------+------------------------------------------------

Comment(by oliphant):

Thanks for the more detailed explanation.  I thought you might be
referring to that case.    It is a case we cover to try and avoid the
incorrect but sometimes natural thought of "replace slice with a list"
without also expecting potentially different behavior.

It's just a different interface and a different choice --- and a more
powerful choice as illustrated by several use cases we teach and talk
about.    While some people (especially people used to Matlab or new to
the environment) think of a slice object as equivalent to the range
produced by the slice --- and we even encourage the confusion at times by
using r_, mgrid_ and friends --- the slice and the range are two distinct
things, and so it should not necessarily be "unexpected" if there use in
indexing results in two different outcomes.

Once you learn what is actually happening it is not necessarily
inconsistent or unexpected, and in fact is a very nice feature.   Just
because you were expecting the "cross-product" does not mean that it
*should* be that way.   There are some very nice use-cases of having the
element-by-element multi-index behavior of fancy indexing.   If you want
the cross-product you can get it using the ix_ function (which just uses
broadcasting to re-shape the inputs).  In your example:

a[ix_([0,2,3],[2,1,2])]

would produce a 2-d array that you expected.   There were lots of
discussions about this before NumPy 1.0 and we kept the behavior of
Numarray.  The big thing NumPy did was allow the mixing of lists and
slices at all.

In sum, I am very much opposed to changing fancy indexing or moving it to
the "take" method.   There are some very nice use-cases of the current
behavior and it in fact is one of the "features" of NumPy that I advertise
quite often to other people.

There is one use-case of mixed fancy indexing and slices that should be
changed in NumPy 2.0 which it would take too long to describe here but it
basically causes output dimensions to be swapped in ways that could be
avoided under specific but useful use-cases.

--
Ticket URL: <http://projects.scipy.org/numpy/ticket/402#comment:11>
NumPy <http://projects.scipy.org/numpy>
My example project
```