On 05/12/2010 12:37 PM, Gregory, Matthew wrote:
Apologies for what is likely a simple question and I hope it hasn't been asked before ...
Given a recarray with a dtype consisting of more than one type, e.g.
import numpy as n a = n.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) b = a.view(n.recarray) b rec.array([(1.0, 2), (3.0, 4)], dtype=[('x', '
Is there a simple way to convert 'b' to a floating-point ndarray, casting the integer field to a floating-point? I've tried the naïve:
c = b.view(dtype='float').reshape(b.size,-1)
but that fails with:
ValueError: new type not compatible with array.
I understand why this would fail (as it is a view and not a copy), but I'm lost on a method to do this conversion simply.
It may not be as simple as you would like, but the following works efficiently: import numpy as np a = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)]) b = np.empty((a.shape[0], 2), dtype=np.float) b[:,0] = a['x'] b[:,1] = a['y'] Eric
thanks, matt