Cannot store Sequences in Numeric arrays of Objects

Tim Hochberg hochberg at psn.net
Tue Jun 1 21:00:25 EDT 1999


Stuart I Reynolds wrote in message <375453CE.3192 at cs.bham.ac.uk>...


[Stuart demonstrates that Numeric doesn't support PyObjects very well.
 In particular you can't store a sequence into an array of PyObjects]

The problem that you're running into is that Numeric is trying to be too
helpful! When you assign a sequence to an array it tries to unpack it into
the array. This works great under "normal" circumstances when you are using
Numeric arrays for numeric types. For example:

>>> a = zeros((3,3), Int)
>>> a[0] = (1,5,9)
>>> a
array([[1, 5, 9],
       [0, 0, 0],
       [0, 0, 0]])

Unfortunately when you try to store a sequence of PyObjects into a slot of
an array, it trys to unpack the sequence, decides that sequence is too long
for the slot and fails like so:

>>> b = zeros((3,3), PyObject)
>>> b[0,0] = (1,2,3)
Traceback (innermost last):
  File "<pyshell#6>", line 1, in ?
    b[0,0] = (1,2,3)
ValueError: array too large for destination

Numeric could probably be smarter about how it treats PyObjects, but as far
as I know, the PyObject feature is used very little.

-tim

PS I could swear I used to know a work around for this, but I sure can't
remember it now.






More information about the Python-list mailing list