[Numpy-discussion] Question regarding documentation of structured arrays

Hanno Klemm klemm at phys.ethz.ch
Wed Jul 24 05:46:25 EDT 2013


Hi,

I found the following inconsistency between the advertised and the 
actual behviour of structured arrays:

on http://docs.scipy.org/doc/numpy/user/basics.rec.html it says in the 
section

"Accessing multiple fields at once"
Notice that the fields are always returned in the same order regardless 
of the sequence they are asked for.

Fortunately that does not seem to be the case in my simple test (see 
below). Is that a change in behaviour I can rely on or am I somehow 
lucky in this particular example?

Thanks,
Hanno


In [596]: test_array = np.ones((10),dtype=[('a', float), ('b',float)])

In [597]: test_array
Out[597]:
array([(1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (1.0, 1.0),
        (1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (1.0, 1.0)],
       dtype=[('a', '<f8'), ('b', '<f8')])

In [598]: test_array['b']*=2

In [599]: test_array
Out[599]:
array([(1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0),
        (1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0)],
       dtype=[('a', '<f8'), ('b', '<f8')])

In [600]: test_array[['b','a']]
Out[600]:
array([(2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0),
        (2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0)],
       dtype=[('b', '<f8'), ('a', '<f8')])

In [601]: test_array[['a','b']]
Out[601]:
array([(1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0),
        (1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0), (1.0, 2.0)],
       dtype=[('a', '<f8'), ('b', '<f8')])

In [602]: test_ab = test_array[['a','b']]

In [603]: test_ba = test_array[['b','a']]

In [604]: test_ba
Out[604]:
array([(2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0),
        (2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0), (2.0, 1.0)],
       dtype=[('b', '<f8'), ('a', '<f8')])

In [605]: np.__version__
Out[605]: '1.6.1'


-- 
Hanno Klemm
klemm at phys.ethz.ch



More information about the NumPy-Discussion mailing list