# [Numpy-discussion] non-integer index misfeature?

Nathaniel Smith njs@pobox....
Wed Dec 12 15:56:32 CST 2012

```On Wed, Dec 12, 2012 at 9:09 PM, David Warde-Farley
<d.warde.farley@gmail.com> wrote:
> On Wed, Dec 12, 2012 at 3:20 PM, Ralf Gommers <ralf.gommers@gmail.com> wrote:
>> For numpy indexing this may not be appropriate though; checking every index
>> value used could slow things down and/or be quite disruptive.
>
> For array fancy indices, a dtype check on the entire array would
> suffice. For lists and scalars, I doubt it'd be substantial amount of
> not totally sure whether these are coerced to arrays first -- if they
> are, the dtype check need only be performed once after that). At any
> rate, a benchmark of any proposed solution is in order.

The current behaviour seems to be:

Scalars are silently cast to int:

In [6]: a = np.arange(10)
In [7]: a[1.5]
Out[7]: 1

So are lists:

In [8]: a[[1.5, 2.5]]
Out[8]: array([1, 2])

In fact, it looks like lists always get passed through
np.array(mylist, dtype=int), because even a list of booleans gets cast
to integer:

In [21]: a[[False] * 10]
Out[21]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

In [10]: a[np.array([1.5, 2.5])]
IndexError: arrays used as indices must be of integer (or boolean) type

Complex scalars are also cast (but with a warning):

In [13]: a[np.complex64(1.5)]
/home/njs/.user-python2.7-64bit/bin/ipython:1: ComplexWarning: Casting
complex values to real discards the imaginary part
Out[13]: 1

So now I'm even more in favour of making floating point indexes a hard
error (with a deprecation period).
https://github.com/numpy/numpy/issues/2810

Also we should fix it so that a[list] acts exactly like
a[np.array(list)]. (Again with a suitable deprecation period.) The
current behaviour is really weird!
https://github.com/numpy/numpy/issues/2811

-n