How to create a numpy dtype from other dtypes?
<p>I normally create numpy dtypes like this:  </p>

<pre><code>C = np.dtype([('a',int),('b',float)])
</code></pre>

<p>However in my code I also use the fields <code>a</code> and <code>b</code> individually elsewhere:  </p>

<pre><code>A = np.dtype([('a',int)])
B = np.dtype([('b',float)])
</code></pre>

<p>For maintainability I'd like to derive <code>C</code> from types <code>A</code> and <code>B</code> somehow like this:  </p>

<pre><code>C = np.dtype([A,B])    # this gives a TypeError
</code></pre>

<p>Is there a way in numpy to create complex dtypes by combining other dtypes?</p>

<p>There is a backwater module in <code>numpy</code> that has a bunch of structured/rec array utilities.</p>

<p><code>zip_descr</code> does this sort of <code>descr</code> concatenation, but it starts with arrays rather than the <code>dtypes</code>:</p>

<pre><code>In [77]: import numpy.lib.recfunctions as rf
In [78]: rf.zip_descr([np.zeros((0,),dtype=A),np.zeros((0,),dtype=B)])
Out[78]: [('a', '&lt;i4'), ('b', '&lt;f8')]
In [81]: rf.zip_descr([np.array((0,),dtype=A),np.array((0,),dtype=B)])
Out[81]: [('a', '&lt;i4'), ('b', '&lt;f8')]
</code></pre>

python numpy numpy-dtype