[Numpy-discussion] From float to records

Pierre GM pgmdevlist@gmail....
Thu May 29 15:05:25 CDT 2008


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 ?

Thanks a lot in advance.





More information about the Numpy-discussion mailing list