[Numpy-discussion] Tuples vs. lists when defining recarrays with array()

David Warde-Farley dwf@cs.toronto....
Sun Jun 7 18:31:19 CDT 2009


A question was raised on the #scipy IRC earlier today, about the  
behaviour of array() with structured dtypes. After some educated  
guessing I figured out that for record arrays, tuples (rather than  
lists) must be used to indicate atomic elements. What I wondered is  
whether this behaviour is documented anywhere, and does it belong in  
the array() docstring, for example? The docstring currently reads "...  
or any (nested) sequence."

In [57]: desc0
Out[57]: dtype([('id', '|O4'), ('val', '|O4'), ('date', '|O4')])

In [58]: values0
Out[58]: [9L, 1L, datetime.date(2009, 6, 7)]

In [59]: arr = array(values0, dtype=desc0)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call  
last)

/Users/dwf/<ipython console> in <module>()

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

In [60]: arr = array(tuple(values0), dtype=desc0)

In [61]: arr
Out[61]:
array((9L, 1L, datetime.date(2009, 6, 7)),
       dtype=[('id', '|O4'), ('val', '|O4'), ('date', '|O4')])


- David


More information about the Numpy-discussion mailing list