[Numpy-discussion] About np array and recarray

Yan Tang tang.yan@gmail....
Wed Mar 21 23:48:26 CDT 2012


I am really confused on the np array or record array, and cannot understand
how it works.

What I want to do is that I have a normal python two dimensional array/list:

a = [['2000-01-01', 2],['2000-01-02', 3]]

I want to convert it to a recarray with this dtype [('date', 'object'),
('count', 'int')].  I tried multiple ways and none of them works.  And some
of the tests show pretty odd behavior.

This is good, and it is almost what i want:

>>> import numpy as np
>>> a = [('2000-01-01', 2), ('2000-01-02', 3)]
>>> np.array(a, dtype=[('date', 'object'), ('count', 'int')])
array([('2000-01-01', 2), ('2000-01-02', 3)],
      dtype=[('date', '|O8'), ('count', '<i8')])

Why this doesn't work?!

>>> a = [['2000-01-01', 2],['2000-01-02', 3]]
>>> np.array(a, dtype=[('date', 'object'), ('count', 'int')])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tried to set void-array with object members using buffer.

Why can this cause segmentation fault?!

>>> a = [['2000-01-01', 2],['2000-01-02', 3]]
>>> np.ndarray((len(a),), buffer=np.array(a), dtype=[('date', 'object'),
('count', 'int')])
Segmentation fault (And python quit!)

Python version 2.6.5

On this reference page,

>>> x = np.array([(1,2),(3,4)])
>>> x
array([[1, 2],
       [3, 4]])
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

Can anyone help me about this?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120322/7169fd91/attachment.html 

More information about the NumPy-Discussion mailing list