[Numpy-discussion] Python scalar float indices into array work - but not for array indices - why ?

Sebastian Haase seb.haase@gmail....
Wed Nov 17 12:32:33 CST 2010

On Wed, Nov 17, 2010 at 7:26 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Wed, Nov 17, 2010 at 12:20, Sebastian Haase <seb.haase@gmail.com> wrote:
>> Hi,
>>>>> import numpy as np
>>>>> a = np.arange(4)
>>>>> a[1.8]
>> 1
>>>>> a[ np.array(1.8) ]
>> Traceback (most recent call last):
>>  File "<input>", line 1, in <module>
>> IndexError: arrays used as indices must be of integer (or boolean) type
>> Why does numpy not accept float arrays as indices ?
>> I was very happy and quite surprised once I found out that it worked
>> at all for Python float scalars,
>> but would it not just be consequent to also allow float ndarrays then ?
> It only works for float scalars by accident. Do not rely on it.

Could you be more specific ?  As a feature, it for sure can be useful.

Alternatively, could one think of some kind of index generator
functions that could be used to still use float arrays as indices,
without the need for an extra int-valued copy (in memory) of the
original and without considerable speed penalty ?

Thanks for your reply,

More information about the NumPy-Discussion mailing list