numpy array as datatype in a structured array?
<p>I was wondering if it is possible to have a numpy.array as a datatype in a structured array. This is the idea:</p>

<pre><code>import numpy

raw_data = [(1, numpy.array([1,2,3])), 
            (2, numpy.array([4,5,6])), 
            (3, numpy.array([7,8,9]))]

data = numpy.array(raw_data, dtype=[('num', float),
                                    ('arr', numpy.array)])
</code></pre>

<p>I have a list of tuples consisting of an integer and an array and would like to turn this into a structured array. Right now, Python complains that it does not understand the 'numpy.array' datatype. Is there another way to refer to the array datatype?</p>

<p>The motivation behind is to be able to do things like:</p>

<pre><code>print numpy.min(data['arr'], axis=0)
print numpy.min(data['arr'], axis=1)
</code></pre>

<p>and other operations.</p>

<p>Yes, you can create compound fields that look like arrays within the structured array; for example:</p>

<pre><code>import numpy as np
raw_data = [(1, np.array([1,2,3])), 
            (2, np.array([4,5,6])), 
            (3, np.array([7,8,9]))]

tp = np.dtype([('id', int), ('arr', float, (3,))])

x = np.array(raw_data, dtype=tp)
</code></pre>

<p>Result looks like this:</p>

<pre><code>&gt;&gt;&gt; x
array([(1, [1.0, 2.0, 3.0]), (2, [4.0, 5.0, 6.0]), (3, [7.0, 8.0, 9.0])], 
      dtype=[('id', '&lt;i8'), ('arr', '&lt;f8', (3,))])
</code></pre>

python arrays numpy structured-array