I have a numpy array with 8 16 byte long void records that looks like this:
array([[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]],
dtype='|V16')
And need to cast it to an array with 8 16B long records of a custom dtype that looks like this:
[(('x', 's0'), '<u4'), (('y', 's1'), '<u4'), (('z', 's2'), '<u4'), ('padding0', '<u4')]
How can I achieve that?
I've tried array.astype(self.dtype, copy=False, casting="unsafe")
,
but I get
ValueError: setting an array element with a sequence.
Which doesn't make much sense to me.
This data comes from PyOpenCL (memory mapping a buffer), I can't really change the input format or the dtype.
As long as the number of bytes match,
view
can make this kind of transformation. It just changes how the data buffer is 'viewed'.I often have to experiment to figure out whether
astype
orview
is the right way to transform a structured array.