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