I am trying to create a structured array in the below format:
import numpy as np
x = np.array([(2009, ((\'USA\', 10.), (\'CHN\', 12.))), (2010, ((\'BRA\', 10
You need to give the second element of your dtype a name, try:
>>> dtype=[('year', '<i4'), ('item_name', [('iso','a3'), ('value','<f4')])]
>>> np.zeros(3, dtype=dtype)
array([(0, ('', 0.0)), (0, ('', 0.0)), (0, ('', 0.0))],
dtype=[('year', '<i4'), ('item_name', [('iso', '|S3'), ('value', '<f4')])])
Forgive me for editorializing, but I find rec-arrays hard enough to work with without the nesting, would you loose a lot if you just flattened the dtype?
update:
You have one more level of nesting than I realized. Try this:
>>> dtype=[('year', '<i4'), ('countries', [('c1', [('iso','a3'), ('value','<f4')]), ('c2', [('iso','a3'), ('value','<f4')])])]
>>> np.array([(2009, (('USA', 10.), ('CHN', 12.))), (2010, (('BRA', 10.), ('ARG', 12.)))], dtype)
array([(2009, (('USA', 10.0), ('CHN', 12.0))),
(2010, (('BRA', 10.0), ('ARG', 12.0)))],
dtype=[('year', '<i4'), ('countries', [('c1', [('iso', '|S3'), ('value', '<f4')]), ('c2', [('iso', '|S3'), ('value', '<f4')])])])