[Numpy-discussion] Scalars, comparisons and indexing. Is this a bug?

Fernando Perez fperez.net at gmail.com
Fri Jul 14 19:52:58 CDT 2006

Hi all,

I just got bit by this problem, and I'm not really sure if this is
something that should be considered a numpy bug or not.  It is a bit
unpleasant, at the least, though it can be easily worked around.
Here's a simple demonstration:

In [32]: a=array([1,2])

In [33]: b=1

In [34]: (1,2)[a[0]==b]
exceptions.TypeError                                 Traceback (most
recent call last)

/home/fperez/research/code/mwmerge/mwadap-merge/mwadap/test/<ipython console>

TypeError: tuple indices must be integers

Whereas this works:

In [35]: c=2

In [36]: (1,2)[c==b]
Out[36]: 1

Basically, it seems that array scalars, upon comparisons, return
something that doesn't 'looke enough like an int' to python for it to
let you use it as an index, it's a 'boolscalar':

In [38]: a0==b0
Out[38]: True

In [39]: type _
-------> type(_)
Out[39]: <type 'boolscalar'>

Advice? Is this a numpy bug?  Or should it just be left alone, and
this kind of inconvenience will disappear when 2.5 is out, with
__index__ support?



More information about the Numpy-discussion mailing list