[Numpy-discussion] dumb structured arrays question

Erin Sheldon erin.sheldon@gmail....
Thu Sep 17 17:18:29 CDT 2009


On Thu, Sep 17, 2009 at 6:12 PM, David Warde-Farley <dwf@cs.toronto.edu> wrote:
> If I have a 1-dimensional array with a structured dtype, say str,
> float, float, float, float.... where all the float columns have their
> own names, and I just want to extract all the floats in the order they
> appear into a 2D matrix that disregards the dtype metadata... Is there
> an easy way to do that?
>
> Currently the only thing I can think of is
>
> out = []
> for name in arr.dtype.names:
>        if name != 'condition': # my single str field
>                out.append(arr['name'])
> out = np.array(out)
>
> Is there already some convenience function for this, to coerce numeric
> compound dtypes into an extra dimension?
>
> It seems conceivable that I might be able to pull some dirty trick
> with stride_tricks that would even allow me to avoid the copy, since
> it'd just be a matter of an extra few bytes on the 2nd dimension
> stride to skip over the string data.
>
> Thanks,
>
> David

You can just view it differently:

In [4]: x=numpy.zeros(3,dtype=[('field1','S5'),('field2','f4'),('field3','f4'),('field4','f4')])

In [5]: x
Out[5]:
array([('', 0.0, 0.0, 0.0), ('', 0.0, 0.0, 0.0), ('', 0.0, 0.0, 0.0)],
      dtype=[('field1', '|S5'), ('field2', '<f4'), ('field3', '<f4'),
('field4', '<f4')])

In [6]: dt=numpy.dtype([('field1','S5'),('compound','3f4')])

In [7]: x.view(dt)
Out[7]:
array([('', [0.0, 0.0, 0.0]), ('', [0.0, 0.0, 0.0]), ('', [0.0, 0.0, 0.0])],
      dtype=[('field1', '|S5'), ('compound', '<f4', 3)])

In [8]: x.view(dt)['compound']
Out[8]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]], dtype=float32)

Hope this helps,
Erin Sheldon


More information about the NumPy-Discussion mailing list