[Numpy-discussion] indexing bug?

Stefan van der Walt stefan@sun.ac...
Wed Oct 3 12:35:46 CDT 2007


On Wed, Oct 03, 2007 at 11:12:07AM -0400, Perry Greenfield wrote:
>
> On Sep 28, 2007, at 4:23 PM, Stefan van der Walt wrote:
>
>> On Fri, Sep 28, 2007 at 03:07:30PM -0400, Nadia Dencheva wrote:
>>> This should return an error and not silently truncate to int.
>>
>> Why do you say that?  The current behaviour is consistent and well
>> defined:
>>
>> a[x] == a[int(x)]
>>
> I disagree. It is neither consistent nor well defined.

It works for other objects too:

class MyIndex(object):
    def __int__(self):
        return 1

m = M()

x = N.array([1,2,3]

x[m] == x[1]

> It is not consistent with Python list indexing behavior.

Neither are most other forms of ndarray indexing:

x = [1,2,3]
x[[1,2]]

yields an error.

> It is not consistent with numpy behavior:
>
> >>> x = arange(10)
> >>> x[array(2.99)]
>
> raises an exception

That seems to be an exception to the rule.  I agree that both

x[2.99] and x[array(2.99)]

should behave the same way.

>> We certainly can't change it now (just imagine all the code out there
>> that will break); but I personally don't think it is a big problem.
>>
> I disagree. If people are willing to change the Class API of numpy to be 
> consistent with Python, they certainly should be willing to change this. 
> This behavior is new with numpy, so there should not be a lot of code that 
> depends on it (and shame on those that do :-).

Let me rephrase: we cannot change the API until 1.1, unless this is
seen as a bug.  To which other API changes are you referring?  The
style changes is a different matter entirely.

My point was that the current behaviour is easy to predict, but I am
not especially partial on the matter.

Regards
Stéfan


More information about the Numpy-discussion mailing list