[SciPy-User] best way to convert a structured array to a float view (again)

Skipper Seabold jsseabold@gmail....
Fri Jun 4 14:43:18 CDT 2010


On Fri, Jun 4, 2010 at 3:38 PM, Vincent Davis <vincent@vincentdavis.net> wrote:
> On Fri, Jun 4, 2010 at 9:55 AM, Skipper Seabold <jsseabold@gmail.com> wrote:
>> Say I have the following arrays that I want to view as/cast to plain
>> ndarrays with float dtype
>>
>> import numpy as np
>> arr = np.array([(24,),(24,),(24,),(24,),(24,)], dtype=[("var1",int)])
>>
>> arr2 = np.array([(24,4.5),(24,4.5),(24,4.5),(24,4.5),(24,4.5)],
>> dtype=[("var1",int),("var2",float)])
>>
>> What I really want to be able to do is something like
>>
>> arr.view(float)
>
> I am going to do some timing but this looks promising. Glad to know I
> am not the onlyone that think going between data types is a hassel.
>
> arr2 = np.array([(24,4.5),(24,4.5),(24,4.5),(24,4.5),(24,4.5)],
>                dtype=[("var1",int),("var2",float)])
>>>> arr2.dtype=float
>>>> arr2
> array([  1.18575755e-322,   4.50000000e+000,   1.18575755e-322,
>         4.50000000e+000,   1.18575755e-322,   4.50000000e+000,
>         1.18575755e-322,   4.50000000e+000,   1.18575755e-322,
>         4.50000000e+000])
>
> Of course if you want to leave arr2 untouched you need some type of copy.
>

Yeah, you can't do it in place.  The int data gets turned to garbage
there, which is not what I want.

Skipper


More information about the SciPy-User mailing list