Numpy array of numpy arrays with certain format
<p>I want to declare a numpy array (<code>arr</code>) with elements having a certain format, like this:</p>

<pre><code>dt1 = np.dtype([('sec', '&lt;i8'), ('nsec', '&lt;i8')])
dt2 = np.dtype([('name', 'S10'), ('value', '&lt;i4')])

arr = np.array([('x', dtype=dt1), ('y', dtype=dt2)])
</code></pre>

<p>The structure is something like this:</p>

<pre><code>arr['x'] = elements with the format dt1 (sec and nsec)
arr['y'] = array of n elements with the format dt2 (name and value)
</code></pre>

<p>And the elements within should be accessed like this:</p>

<pre><code>arr['x']['sec'], arr['x']['nsec'], arr['y'][0]['name'] etc.
</code></pre>

<p>But I get an <code>invalid syntax</code> error.
What is the correct syntax in this situation?</p>

<p>A doubly compound dtype might work:</p>

<pre><code>In [834]: dt1 = np.dtype([('sec', '&lt;i8'), ('nsec', '&lt;i8')])
     ...: dt2 = np.dtype([('name', 'S10'), ('value', '&lt;i4')])
     ...: 
In [835]: dt = np.dtype([('x', dt1),('y', dt2)])

In [837]: z=np.ones((3,), dtype=dt)
In [838]: z
Out[838]: 
array([((1, 1), (b'1', 1)), ((1, 1), (b'1', 1)), ((1, 1), (b'1', 1))], 
      dtype=[('x', [('sec', '&lt;i8'), ('nsec', '&lt;i8')]), ('y', [('name', 'S10'), ('value', '&lt;i4')])])

In [839]: z['x']['sec']
Out[839]: array([1, 1, 1], dtype=int64)

In [841]: z['y'][0]['name']='y0 name'
In [842]: z
Out[842]: 
array([((1, 1), (b'y0 name', 1)), ((1, 1), (b'1', 1)), ((1, 1), (b'1', 1))], 
      dtype=[('x', [('sec', '&lt;i8'), ('nsec', '&lt;i8')]), ('y', [('name', 'S10'), ('value', '&lt;i4')])])
In [843]: z[0]
Out[843]: ((1, 1), (b'y0 name', 1))
</code></pre>

<p>A dictionary would work with similar syntax</p>

<pre><code>In [845]: d={'x':np.ones((3,),dt1), 'y':np.zeros((4,),dt2)}
In [846]: d
Out[846]: 
{'x': array([(1, 1), (1, 1), (1, 1)], 
       dtype=[('sec', '&lt;i8'), ('nsec', '&lt;i8')]),
 'y': array([(b'', 0), (b'', 0), (b'', 0), (b'', 0)], 
       dtype=[('name', 'S10'), ('value', '&lt;i4')])}
In [847]: d['y'][0]['name']='y0 name'
</code></pre>

python arrays numpy syntax