this code (snippet_1) is to construct a structured array
>>> dt = np.dtype([('name', np.str_, 16), ('age', np.int)])
>>> x = np.array([('Sarah', 16), ('John', 17)], dtype=dt)
>>> x
array([('Sarah', 16), ('John', 17)],
dtype=[('name', '<U16'), ('age', '<i8')])
this code is to set dtype to a given simple array
arr = np.array([10, 20, 30, 40, 50])
arr = arr.astype('float64')
this code (snippet_3) is trying to set dtype to a structured array,
x = np.array([('Sarah', 16), ('John', 17)])
x = x.astype(dt)
of course, set dtype this way causes ValueError
ValueError Traceback (most recent call last)
<ipython-input-18-201b69204e82> in <module>()
1 x = np.array([('Sarah', 16), ('John', 17)])
----> 2 x = x.astype(dt)
ValueError: invalid literal for int() with base 10: 'Sarah'
Is it possible to set dtype to an existing structured array? something like snippet_3?
Why would I want to do this? Because there is a handy approach to access data in the setting of snippet_1.
x['name']
If I can add "column name" to an existing array, that would be cool.