[Numpy-discussion] dtype confusion
Stefan van der Walt
Sun Mar 25 07:25:11 CDT 2007
On Sun, Mar 25, 2007 at 04:09:11AM -0700, Jan Strube wrote:
> There seems to be a fundamental lack of understanding on my behalf when it
> comes to dtypes and record arrays.
> Please consider the following snippet:
> import numpy as N
> newtype = N.dtype([('x', N.float64 ), ('y', N.float64), ('z', N.float64)])
> a = N.random.random((100,3))
> b = N.column_stack([a['x'].ravel(), a['y'].ravel(), a['z'].ravel()])
> b.dtype = newtype
> --> ValueError: new type not compatible with array.
> I don't understand two things about this:
> i) the shape of a changes from (100,3) to (100,1) after assigning
> the dtype.
Every group of three floats now become one element in the new array,
float64 -> (float64,float64,float64)
> ii) the shape of b is obviously (100,3), so why can't I assign the
> new dtype?
The array is no longer a contiguous block of memory, so the new dtype
can't be applied:
In : b.flags
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
The following does a copy of the array to contiguous memory:
In : N.ascontiguousarray(b).dtype = newtype
If you want to move back to the original view you can do
More information about the Numpy-discussion