argmax() return type can't index python lists
Hi folks, I'm using a fresh build of numpy and python 2.5, both checked out from SVN yesterday. It seems that numpy.argmax() and ndarray.argmax() are now returning 'int32scalar' types instead of 'int' types. Despite the fact that the former seems to inherit from the latter, 'int32scalar' types do not appear to be usable as indices into python lists. Anyhow, I'm not sure if this is a python 2.5 regression, or a problem in all versions of python that's exposed by this behavior, or a numpy problem. Any thoughts? Zach Test case: In [1]: import numpy In [2]: l = [1,2,3] In [3]: l[numpy.argmax(l)] <class 'exceptions.TypeError'>: list indices must be integers In [4]: type(numpy.argmax(l)) Out[4]: <type 'int32scalar'> In [5]: print numpy.version.version 0.9.6.2208 In [6]: import sys; print sys.version 2.5a0 (trunk:42924M, Mar 8 2006, 19:29:24) [GCC 4.0.1 (Apple Computer, Inc. build 5250)]
Zachary Pincus wrote:
Hi folks,
I'm using a fresh build of numpy and python 2.5, both checked out from SVN yesterday. It seems that numpy.argmax() and ndarray.argmax() are now returning 'int32scalar' types instead of 'int' types. This has always been the case for numpy so it's not new. Despite the fact that the former seems to inherit from the latter, 'int32scalar' types do not appear to be usable as indices into python lists. This is new and is a Python 2.5 problem it would appear.
Anyhow, I'm not sure if this is a python 2.5 regression, or a problem in all versions of python that's exposed by this behavior, or a numpy problem.
Note that the __index__ PEP I wrote was just accepted and checkedin so in fact *all* numpy scalar types will be acceptable as indices in Python2.5 (but we have to write the code for that still in numpy). So, it's possible that something in Python 2.5 changed is causing it not to work. This verywell could be a bug in the new implementation of __index__. Travis
Thanks for your reply, Travis.
Note that the __index__ PEP I wrote was just accepted and checked in so in fact *all* numpy scalar types will be acceptable as indices in Python2.5 (but we have to write the code for that still in numpy). So, it's possible that something in Python 2.5 changed is causing it not to work.
This verywell could be a bug in the new implementation of __index__.
The problem is that I can't reproduce this bug outside of numpy. If I go to the python people, they'll most likely say it's a numpy problem, unless I can show some other test case. Right now, all of the following classes work as indices in 2.5. The only thing that doesn't work is a raw numpy.int32 value. I'd be happy to file a bug report on python if I could figure out a test case. Zach Cases which work fine: class int_sublcass(int): pass class empty(object): pass class int_1(int, empty): pass class int_2(empty, int): pass class int_numpy_1(numpy.signedinteger, int): def __init__(self, i): int.__init__(self, i) class int_numpy_2(int, numpy.signedinteger): def __init__(self, i): int.__init__(self, i) l = [1,2,3] l[int_sublcass(1)] l[int_1(1)] l[int_2(1)] l[int_numpy_1(1)] l[int_numpy_2(1)]
participants (2)

Travis Oliphant

Zachary Pincus