1

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.

1
  • Why do you want to set the dtype of existing structured array? The dtype is an attribute of the array. You can GET it, but I don't think you can change it, especially if it already has data in it. Commented Oct 8, 2019 at 0:38

2 Answers 2

1

You can use numpy.lib.recfunctions.unstructured_to_structured

x = np.array([('Sarah', 16), ('John', 17)])
x
# array([['Sarah', '16'],
#        ['John', '17']], dtype='<U5')
dt = np.dtype([('name', np.str_, 16), ('age', np.int)])

import numpy.lib.recfunctions as nlr

xs = nlr.unstructured_to_structured(x, dtype=dt)
xs
# array([('Sarah', 16), ('John', 17)],
#       dtype=[('name', '<U16'), ('age', '<i8')])
Sign up to request clarification or add additional context in comments.

Comments

0

Given the following input:

dt = np.dtype([('name', np.str_, 16), ('age', np.int)])
x = np.array([('Sarah', 16), ('John', 17)], dtype=dt)

# array([('Sarah', 16), ('John', 17)],
#      dtype=[('name', '<U16'), ('age', '<i8')])

Let's add a column to the structured array, like this:

new_dt = np.dtype(x.dtype.descr + [('weight', float)])
xx = np.zeros(x.shape, dtype=new_dt)

xx['name'] = x['name']
xx['age'] = x['age']

xx['weight'] = [86.7, 78.9]

and gives:

# array([('Sarah', 16, 86.7), ('John', 17, 78.9)],
#      dtype=[('name', '<U16'), ('age', '<i8'), ('weight', '<f8')])

To change the type of data within the structured array instead look at the answer of @PaulPanzer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.