[Numpy-discussion] From float to records

Charles R Harris charlesr.harris@gmail....
Thu May 29 15:25:24 CDT 2008


On Thu, May 29, 2008 at 2:05 PM, Pierre GM <pgmdevlist@gmail.com> wrote:

> All,
> I have a set of arrays that I want to transform to records. Viewing them as
> a
> new dtype is usually sufficient, but fails occasionally. Here's an example:
>
> #---------------------------------------
> import numpy as np
> testdtype = [('a',float),('b',float),('c',float)]
> test = np.random.rand(15).reshape(5,3)
> # View the (5,3) array as 5 records of 3 fields
> newrecord = test.view(testdtype)
> # Create a new array with the wrong shape
> test = np.random.rand(15).reshape(3,5)
> #Try to view it
> try:
>    newrecord = test.T.view(testdtype)
> except ValueError, msg:
>    print "Error creating new record on transpose: %s" % msg
> # That failed, but won't with a copy
> try:
>    newrecord = test.T.copy().view(testdtype)
> except ValueError, msg:
>    print "Error creating new record on transpose+copy: %s" % msg
> #---------------------------------------
>
> * Could somebody explain me what goes wrong in the second case
> (transpose+view) ? Is it because the transpose doesn't own the data ?
>
> * Is there a way to transform my (3,5) array into a (5,) recordarray
> without a
> copy ?
>

I don't think so. The transpose is just a view, it doesn't move the elements
around, =so the three elements you want to be contiguous, aren't. It's
possible to transpose in place, but it can be a tricky operation and I don't
think it is available in numpy.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080529/38533e27/attachment-0001.html 


More information about the Numpy-discussion mailing list