[Numpy-discussion] view of a structured array?

josef.pktd@gmai... josef.pktd@gmai...
Wed Jan 25 13:33:34 CST 2012

```On Wed, Jan 25, 2012 at 2:27 PM, Chris Barker <chris.barker@noaa.gov> wrote:
> HI folks,
>
> Is there a way to get a view of a subset of a structured array? I know
> that an arbitrary subset will not fit into the numpy "strides"offsets"
> model, but some will, and it would be nice to have a view:
>
> For example, here we have a stuctured array:
>
> In [56]: a
> Out[56]:
> array([(1, 2.0, 3.0, 4), (7, 8.0, 9.0, 10), (5, 123.4, 7.0, 8),
>       (9, 10.0, 11.0, 12), (13, 14.0, 15.0, 16)],
>      dtype=[('i', '<i4'), ('f1', '<f8'), ('f2', '<f8'), ('i2', '<i4')])
>
>
> if I pull out one "field" a get a view:
>
> In [57]: b = a['f1']
>
> In [58]: b[0] = 1000
>
> In [59]: a
> Out[59]:
> array([(1, 1000.0, 3.0, 4), (7, 8.0, 9.0, 10), (5, 123.4, 7.0, 8),
>       (9, 10.0, 11.0, 12), (13, 14.0, 15.0, 16)],
>      dtype=[('i', '<i4'), ('f1', '<f8'), ('f2', '<f8'), ('i2', '<i4')])
>
> However, if I pull out more than one field, I get a copy:
>
> In [60]: b = a[['f1','f2']]
>
> In [61]: b
> Out[61]:
> array([(1000.0, 3.0), (8.0, 9.0), (123.4, 7.0), (10.0, 11.0), (14.0, 15.0)],
>      dtype=[('f1', '<f8'), ('f2', '<f8')])
>
> In [62]: b[1] = (2000,3000)
>
> In [63]: b
> Out[63]:
> array([(1000.0, 3.0), (2000.0, 3000.0), (123.4, 7.0), (10.0, 11.0),
>       (14.0, 15.0)],
>      dtype=[('f1', '<f8'), ('f2', '<f8')])
>
> In [64]: a
> Out[64]:
> array([(1, 1000.0, 3.0, 4), (7, 8.0, 9.0, 10), (5, 123.4, 7.0, 8),
>       (9, 10.0, 11.0, 12), (13, 14.0, 15.0, 16)],
>      dtype=[('i', '<i4'), ('f1', '<f8'), ('f2', '<f8'), ('i2', '<i4')])
>
>
> However, in this case, the two fields are contiguous, and thus I'm
> pretty sure one could build a numpy array that was a view. Is there
> any way to do so? Ideally without manipulating the strides by hand,
> but I may want to do that if it's the only way.
>
> -Chris
>

that's what I would try:

>>> b = a.view(dtype=[('i', '<i4'), ('fl',[('f1', '<f8'), ('f2', '<f8')]), ('i2', '<i4')])
>>> b['fl']
array([(2.0, 3.0), (8.0, 9.0), (123.40000000000001, 7.0), (10.0, 11.0),
(14.0, 15.0)],
dtype=[('f1', '<f8'), ('f2', '<f8')])
>>> b['fl'][2]= (200, 500)
>>> a
array([(1, 2.0, 3.0, 4), (7, 8.0, 9.0, 10), (5, 200.0, 500.0, 8),
(9, 10.0, 11.0, 12), (13, 14.0, 15.0, 16)],
dtype=[('i', '<i4'), ('f1', '<f8'), ('f2', '<f8'), ('i2', '<i4')])

Josef
>
>
>
> --
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker@noaa.gov
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
```