[Numpy-discussion] is it safe to change the dtype without rebuilding the array?

Robert Kern robert.kern@gmail....
Fri Jan 1 17:49:31 CST 2010


2010/1/1 Ernest Adrogué <eadrogue@gmx.net>:
> Hi,
>
> I find myself doing this:
>
> In [244]: x
> Out[244]:
> array([[0, 1, 2],
>       [3, 4, 5],
>       [6, 7, 8]])
>
> In [245]: y=x.copy()
>
> In [251]: y.dtype.char
> Out[251]: 'l'
>
> In [252]: dt=np.dtype([('a','l'),('b','l'),('c','l')])
>
> In [254]: y.dtype=dt
>
> Is it okay?
> The problem is that it's not easy to rebuild the array.
> I tried with:
>
> y.astype(dt)
> np.array(y, dt)
> np.array(y.tolist(), dt)
>
> None worked.

y.view(dt)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list