Question

In the section Accessing multiple fields at once of numpy docs, says that:

Notice that the fields are always returned in the same order regardless of the sequence they are asked for.

The docs also give a example as following:

>>> x = np.array([(1.5,2.5,(1.0,2.0)),(3.,4.,(4.,5.)),(1.,3.,(2.,6.))],
        dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))])
>>> x[['x','y']]
array([(1.5, 2.5), (3.0, 4.0), (1.0, 3.0)],
     dtype=[('x', '<f4'), ('y', '<f4')])
>>> x[['y','x']]
array([(1.5, 2.5), (3.0, 4.0), (1.0, 3.0)],
     dtype=[('x', '<f4'), ('y', '<f4')])

However, I have run the code myself with numpy 1.6.1 and got a different result:

In [20]: x = np.array([(1.5,2.5,(1.0,2.0)),(3.,4.,(4.,5.)),(1.,3.,(2.,6.))],
   ....:         dtype=[('x','f4'),('y',np.float32),('value','f4',(2,2))])

In [21]: x[['x','y']]
Out[21]:
array([(1.5, 2.5), (3.0, 4.0), (1.0, 3.0)],
      dtype=[('x', '<f4'), ('y', '<f4')])

In [22]: x[['y','x']]
Out[22]:
array([(2.5, 1.5), (4.0, 3.0), (3.0, 1.0)],
      dtype=[('y', '<f4'), ('x', '<f4')])

Did this behavior changed from numpy 1.6 to 1.7 or I have missed something?

EDIT I have tested the code in numpy 1.7.1, the result was the same as numpy 1.6.

Was it helpful?

Solution

Looks as if the docs are just out of date.

The code which produces the resulting array is in the function _index_fields() in numpy/core/_internal.py.

There was a change between v1.5 and v1.6 on March 22nd, 2011 from...

new_dtype = [(name, dt[name]) for name in dt.names if name in fields]

...to...

new_dtype = [(name, dt[name]) for name in fields if name in dt.names]

...so the order of the fields in the resulting array changed from the order specified in the original data type to the order in which you specified the fields when you accessed them.

However, the section of the documentation cited in the OP was added on March 1st, 2011, three weeks prior to the change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top