I am running Numpy 1.6 in Python 2.7 and have several 1D arrays that I get from another module. I would like to take these arrays and pack them into a structured array so that I can index the original 1D arrays by name. I am having trouble figuring out how to get 1D arrays into a 2D array and get dtype to access the correct data. My MWE is as follows:
>>> import numpy as np >>> >>> x = np.random.randint(10,size=3) >>> y = np.random.randint(10,size=3) >>> z = np.random.randint(10,size=3) >>> x array([9, 4, 7]) >>> y array([5, 8, 0]) >>> z array([2, 3, 6]) >>> >>> w = np.array([x,y,z]) >>> w.dtype=[('x','i4'),('y','i4'),('z','i4')] >>> w array([[(9, 4, 7)], [(5, 8, 0)], [(2, 3, 6)]], dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')]) >>> w['x'] array([[9], [5], [2]]) >>> >>> u = np.vstack((x,y,z)) >>> u.dtype=[('x','i4'),('y','i4'),('z','i4')] >>> u array([[(9, 4, 7)], [(5, 8, 0)], [(2, 3, 6)]], dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')]) >>> u['x'] array([[9], [5], [2]]) >>> v = np.column_stack((x,y,z)) >>> v array([[(9, 4, 7), (5, 8, 0), (2, 3, 6)]], dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')]) >>> v.dtype=[('x','i4'),('y','i4'),('z','i4')] >>> v['x'] array([[9, 5, 2]])
As you can see, while my original x array contains [9,4,7] , I did not try to stack the arrays in any way, and then indexing with 'x' returns the original x array. Is there a way to do this, or am I not mistaken?