[Numpy-discussion] Forcing new dimensions to appear at front in advanced indexing

Sebastian Berg sebastian at sipsolutions.net
Wed Jun 20 09:30:49 EDT 2018


On Wed, 2018-06-20 at 09:15 -0400, Michael Lamparski wrote:
> > There is a way that will generally work using triple indexing:
> >
> > arr[..., None, None][orig_indx + (slice(None), np.array(0))][...,
> 0]
> 
> Impressive! (note: I fixed the * typo in the quote)
> 
> > The first and last indexing operation is just a view creation, so
> it is
> > basically a no-op. Now doing this gives me the shiver, but it will
> work
> > always. If you want to have a no-copy behaviour in case your
> original
> > index is ont an advanced indexing operation, you should replace the
> > np.array(0) with just 0.
> 
> I agree about the shivers, but any workaround is good to have
> nonetheless.
> 
> If the index is not an advanced indexing operation, does it not
> suffice to simply apply the index tuple as-is?

Yes, with the `np.array(0)` however, the result will forced to be a
copy and not a view into the original array, when writing the line
first I thought of "force advanced indexing", which there is likely no
reason for though.
If you replace it with 0, the result will be an identical view when the
index is not advanced (with only a tiny bit of call overhead).

So it might be nice to just use 0 instead, since if your index is
advanced indexing, there is no difference between the two. But then you
do not have to check if there is advanced indexing going on at all.

Btw. if you want to use it for an object, I might suggest to actually
use:

object.vindex[...]

notation for this logic (requires a slightly annoying helper class).
The NEP is basically just a draft/proposal status, but xarray is
already using that indexing method/property IIRC, so that name is
relatively certain by now.

I frankly am not sure right now if the vindex proposal was with a
forced copy or not, probably it was.

- Sebastian


> 
> Michael
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180620/686bb9a9/attachment.sig>


More information about the NumPy-Discussion mailing list