[Numpy-discussion] Indexing empty dimensions with empty arrays

Ralf Gommers ralf.gommers at googlemail.com
Mon Dec 26 14:56:11 EST 2011


On Mon, Dec 26, 2011 at 8:50 PM, <josef.pktd at gmail.com> wrote:

> On Mon, Dec 26, 2011 at 1:51 PM, Ralf Gommers
> <ralf.gommers at googlemail.com> wrote:
> >
> >
> > 2011/12/25 Jordi Gutiérrez Hermoso <jordigh at octave.org>
> >>
> >> I have been instructed to bring this issue to the mailing list:
> >>
> >>   http://projects.scipy.org/numpy/ticket/1994
> >>
> > The issue is this corner case:
> >
> >>>> idx = []
> >>>> x = np.array([])
> >>>> x[idx]  #works
> > array([], dtype=float64)
> >>>> x[:, idx]  #works
> > array([], dtype=float64)
> >
> >>>> x = np.ones((5,0))
> >>>> x[idx]  #works
> > array([], shape=(0, 0), dtype=float64)
> >>>> x[:, idx]  #doesn't work
> > Traceback (most recent call last):
> >   File "<ipython-input-27-7038691cb565>", line 1, in <module>
> >     x[:, idx]  #doesn't work
> > IndexError: invalid index
> >
> >
> > This is obviously inconsistent, but I think just fixing this one case is
> not
> > enough; unexpected behavior with empty inputs/indexes keeps coming up.
> Do we
> > need a clear set of rules that all functions follow and tests to ensure
> > these rules are actually followed, or not?
>
> this works
> >>> xx = np.arange(12).reshape(3,4)
> >>> xx
> array([[ 0,  1,  2,  3],
>       [ 4,  5,  6,  7],
>       [ 8,  9, 10, 11]])
> >>> x = xx[:,xx[:,-1]<3]
> >>> x
> array([], shape=(3, 0), dtype=int32)
> >>> x<0
> array([], shape=(3, 0), dtype=bool)
> >>> x[x<0]
> array([], dtype=int32)
> >>> x[:,x<0]
> array([], dtype=int32)
>
> >>> x.ndim
> 2
>
> I have a hard time thinking through empty 2-dim arrays, and don't know
> what rules should apply.
> However, in my code I might want to catch these cases rather early
> than late and then having to work my way backwards to find out where
> the content disappeared.
>

Same here. Almost always, my empty arrays are either due to bugs or they
signal that I do need to special-case something. Silent passing through of
empty arrays to all numpy functions is not what I would want.

Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111226/18cb677d/attachment.html>


More information about the NumPy-Discussion mailing list