How do I properly combine two numpy ndarrays that have named fields and are the same length into one ndarray? In my example below I would like to combine xnd and ynd into a single numpy ndarray.
I know how to create a new ndarray from the concatenated dtype of xnd and ynd and then iteratively copy the contents from xnd and ynd into that new ndarray. But is there a numpy command that will do this for me?
The fastest and simplest way of combining xnd and ynd would be ideal. Perhaps appending ynd to xnd inplace rather than making a copy? This solution needs to work fast with large ndarrays.
I have seen several examples on how to combine simple n dimensional numpy arrays, but they haven't helped me with this problem. The line with znd = np.join((xnd, ynd)) at the bottom of my example is where I get stuck.
Thanks!
import numpy as np
n = 10
t = np.arange(n)
abc = np.array((t,t+n,t+2*n)).T
y = (t*10).astype(np.uint32)
# Create x ndarray
xdt = np.dtype([
('t', np.float64),
('abc', (np.float32, 3) )
])
xnd = np.ndarray( shape=n, dtype=xdt)
xnd['t'] = t
xnd['abc'] = abc
# Create y ndarray
ydt = np.dtype([
('y', np.uint32),
])
ynd = np.ndarray( shape=n, dtype=ydt)
ynd['y'] = y
print xnd.dtype
# [('t', '<f8'), ('abc', '<f4', (3,))]
print ynd.dtype
# [('y', '<u4')]
# Combine x and y
# This line not correct. What is the proper way to do this?
znd = np.join((xnd, ynd))
print znd.dtype
# [('t', '<f8'), ('abc', '<f4', (3,)), ('y', '<u4')]