user918967
user918967

Reputation: 2167

Numpy structured array document confusion

I am a total newbie to numpy structured arrays.

If I use the code from the docs (see here: Numpy Structured Array)

import numpy as np 
x=np.array([(1,2.,'Hello'), (2,3.,"World")], dtype=[('foo', 'i4'),('bar', 'f4'), ('baz', 'S10')])
x
y = x['foo']
y

x is correct: array([(1, 2.0, 'Hello'), (2, 3.0, 'World')], dtype=[('foo', '<i4'), ('bar', '<f4'), ('baz', 'S10')])

However y gives me array([1, 2]) and the docs say it should be array([ 2., 3.], dtype=float32)

I have a hard time believing the docs are wrong but this code is so short and and I copied/pasted it into python.

Am I doing something wrong?

Upvotes: 3

Views: 93

Answers (1)

Daniel
Daniel

Reputation: 42748

x['foo'] gives you an array of all first elements of each structure, so [1,2] is correct. The documentation mistakenly switched foo and bar, so read the docs as

y = x['bar']

and the rest of the example is correct.

Upvotes: 2

Related Questions