[Numpy-discussion] view of a structured array?

Chris Barker chris.barker at noaa.gov
Wed Jan 25 14:27:10 EST 2012


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





-- 
--

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 at noaa.gov



More information about the NumPy-Discussion mailing list