mommermi
mommermi

Reputation: 1052

numpy array as datatype in a structured array?

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

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)])

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?

The motivation behind is to be able to do things like:

print numpy.min(data['arr'], axis=0)
print numpy.min(data['arr'], axis=1)

and other operations.

Upvotes: 2

Views: 784

Answers (1)

jakevdp
jakevdp

Reputation: 86320

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

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)

Result looks like this:

>>> 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', '<i8'), ('arr', '<f8', (3,))])

Upvotes: 4

Related Questions