[Numpy-discussion] recarrays and lists of lists

Travis Vaught travis at vaught.net
Sun Sep 11 01:30:23 EDT 2011


Is there a particular reason why a list of lists can't be passed in to create a recarray given a particular dtype?

A list of tuples works fine.  I keep getting bitten by this and was thinking it should be an easy check/convert for an allowance for a row to be a list _or_ a tuple?

Here's a session:


In [2]: import numpy as np

In [3]: dt = np.dtype({'names':['a', 'b', 'c'], 'formats':[float]*3})

In [4]: dt
Out[4]: dtype([('a', '<f8'), ('b', '<f8'), ('c', '<f8')])

In [5]: rows = [[1,2,3],[2,3,4],[3,4,5]]

In [6]: ary = np.array(rows, dtype=dt)
TypeError                                 Traceback (most recent call last)
/Users/travis/git/trading/<ipython-input-6-b69b5c361254> in <module>()
----> 1 ary = np.array(rows, dtype=dt)

TypeError: expected a readable buffer object

In [7]: rows2 = [tuple(row) for row in rows]

In [8]: ary = np.array(rows2, dtype=dt)

In [9]: ary
array([(1.0, 2.0, 3.0), (2.0, 3.0, 4.0), (3.0, 4.0, 5.0)], 
      dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')])





More information about the NumPy-Discussion mailing list