3

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?

1
  • Looks like a documentation error to me. Commented Dec 23, 2015 at 17:32

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Kinda crazy a noob can find this... How does someone go about correcting the docs?
@user918967 You should open a new issue on the numpy GitHub page
docs.scipy.org/doc/numpy/user/index.html warns that the 'user guide' is a work in progress. Evidently developers have put more effort in to the reference section, as well as responding to code errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.