[Numpy-discussion] tried to set void-array with object members using buffer error message

Robert Kern robert.kern at gmail.com
Sat Nov 8 23:14:05 EST 2008


On Sat, Nov 8, 2008 at 18:35, ctw <lists.20.chth at xoxy.net> wrote:
> Hi! Can someone here shed some light on this behavior:
>
>> tst = np.zeros(2,[('a',np.int32),('b','S04')])
>> np.random.shuffle(tst) # this works fine
>
>> tst2 = np.zeros(2,[('a',np.int32),('b',list)])
>> np.random.shuffle(tst2)
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)
>
> /home/ctw/<ipython console> in <module>()
>
> /home/ctw/mtrand.pyx in mtrand.RandomState.shuffle()
>
> ValueError: tried to set void-array with object members using buffer.

Hmm. Odd. Looks like you can't set an element of a structured array
with a structured scalar if one of the fields is an object field.
Seems like a bug.

In [11]: from numpy import *

In [12]: a = zeros(2, [('a', float), ('b', object)])

In [13]: a
Out[13]:
array([(0.0, 0), (0.0, 0)],
      dtype=[('a', '<f8'), ('b', '|O4')])

In [14]: a[0] = a[1]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/rkern/Documents/Freescale/<ipython console> in <module>()

ValueError: tried to set void-array with object members using buffer.

> I get the same error if I use np.ndarray instead of list. Also, when I
> put in a class I locally define I get a "data type not understood"
> error:

Yeah, don't do that. Always use 'object'. numpy does no type-checking
for object arrays/fields. It should probably give you an error for
'list', too.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list