[Numpy-discussion] About np array and recarray

Val Kalatsky kalatsky@gmail....
Thu Mar 22 00:32:15 CDT 2012


Will this do what you need to accomplish?

import datetime
np.array([(datetime.datetime.strptime(i[0], "%Y-%m-%d").date(), i[1]) for i
in a], dtype=[('date', 'object'), ('count', 'int')])

Val

On Wed, Mar 21, 2012 at 11:48 PM, Yan Tang <tang.yan@gmail.com> wrote:

> Hi,
>
> 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,
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html
>
> >>> 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?
>
> Thanks.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120322/5c9515b0/attachment.html 


More information about the NumPy-Discussion mailing list