4

I have a pair of numpy arrays; here's a simple equivalent example:

t = np.linspace(0,1,100)
data = ((t % 0.1) * 50).astype(np.uint16)

I want these to be columns in a numpy recarray of dtype f8, i2. This is the only way I can seem to get what I want:

X = np.array(zip(t,data),dtype=[('t','f8'),('data','i2')])

But is it the right way if my data values are large? I want to minimize the unnecessary overhead of shifting around data.

This seems like it should be an easy problem but I can't find a good example.

1 Answer 1

10

A straight-forward way to do this is with numpy.rec.fromarrays. In your case:

np.rec.fromarrays([t, data], dtype=[('t','f8'),('data','i2')])

or simply

np.rec.fromarrays([t, data], names='t,data', formats='f8,i2')

would work.

Alternative approaches are also given at Converting a 2D numpy array to a structured array

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

Comments

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.