I would like to take an existing array with several named fields, and create a new array (or change it in place) with one field with a hierarchical dtype equal to the original dtype. That is,
newarray = np.array(oldarray, dtype=[('old',oldarray.dtype)])
such that newarray['old']
is identical in shape and structure to oldarray
Here's an example:
In [1]: import numpy as np
In [2]: dt = np.dtype([('name',np.str_,2),('val',np.float_)])
In [3]: constants = np.array([('pi',3.14),('e',2.72)],dtype=dt)
In [4]: constants
Out[4]:
array([('pi', 3.14), ('e', 2.72)],
dtype=[('name', '|S2'), ('val', '<f8')])
In [5]: numbers = constants.astype([('constants',dt)])
But this gives me all zeros:
In [6]: numbers
Out[6]:
array([(('', 0.0),), (('', 0.0),)],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
I have the same problem if I try to make a copy:
In [7]: numbers = np.array(constants,dtype=[('constants',dt)])
In [8]: numbers
Out[8]:
array([(('', 0.0),), (('', 0.0),)],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
Also: Does anybody know why this is happening?