[Numpy-discussion] non-integer index misfeature?
Wed Dec 12 15:56:32 CST 2012
On Wed, Dec 12, 2012 at 9:09 PM, David Warde-Farley
> On Wed, Dec 12, 2012 at 3:20 PM, Ralf Gommers <email@example.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
> overhead compared to the overhead that already exists (for lists, I'm
> 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 : a = np.arange(10)
In : a[1.5]
So are lists:
In : a[[1.5, 2.5]]
Out: 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
In : a[[False] * 10]
Out: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
But arrays must have integer type to start with:
In : 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 : a[np.complex64(1.5)]
/home/njs/.user-python2.7-64bit/bin/ipython:1: ComplexWarning: Casting
complex values to real discards the imaginary part
So now I'm even more in favour of making floating point indexes a hard
error (with a deprecation period).
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!
P.S. to Neal: none of this is going to help your original code though,
because your 'bins' array actually contains integers...
More information about the NumPy-Discussion