[Numpy-discussion] Indexing empty dimensions with empty arrays

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Dec 26 14:50:58 EST 2011


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.

my 2c

Josef
>
> Ralf
>
> _______________________________________________
> 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