[Numpy-discussion] Fancy-indexing reorders output in corner cases?

Eric Firing efiring at hawaii.edu
Tue May 15 14:15:38 EDT 2012

On 05/14/2012 06:03 PM, Travis Oliphant wrote:
> What happens, though when you have
> a[:, in1 :, in2]?
> in1 and in2 are broadcasted together to create a two-dimensional
> "sub-space" that must fit somewhere.   Where should it go?   Should
> it replace in1 or in2?    I.e. should the output be
> (10,3,4,8) or (10,8,3,4).
> To "resolve" this ambiguity, the code sends the (3,4) sub-space to
> the front of the "dimensions" and returns (3,4,10,8).   In
> retro-spect, the code should raise an error as I doubt anyone
> actually relies on this behavior, and then we could have "done the
> right" thing for situations like in1 being an integer which actually
> makes some sense and should not have been confused with the "general
> case"
> In this particular case you might also think that we could say the
> result should be (10,3,8,4) but there is no guarantee that the number
> of dimensions that should be appended by the "fancy-indexing" objects
> will be the same as the number of dimensions replaced.    Again, this
> is how fancy-indexing combines with other fancy-indexing objects.
> So, the behavior is actually quite predictable, it's just that in
> some common cases it doesn't do what you would expect --- especially
> if you think that [0,1] is "the same" as :2.   When I wrote this code
> to begin with I should have raised an error and then worked in the
> cases that make sense.    This is a good example of making the
> mistake of thinking that it's better to provide something very
> general rather than just raise an error when an obvious and clear
> solution is not available.
> There is the possibility that we could now raise an error in NumPy
> when this situation is encountered because I strongly doubt anyone is
> actually relying on the current behavior.    I would like to do this,
> actually, as soon as possible.  Comments?


Good idea, especially if you can then make the integer case work as one 
might reasonably expect.  Keeping the present too-fancy capabilities can 
only cause continuing confusion.


> -Travis

More information about the NumPy-Discussion mailing list