Numpy object array of numerical arrays Numpy object array of numerical arrays numpy numpy

Numpy object array of numerical arrays


It's not exactly pretty, but...

import numpy as npa = np.array([1,2,3])b = np.array([None, a, a, a])[1:]print b.dtype, b[0].dtype, b[1].dtype# object int32 int32


a = np.array([1,2,3])b = np.empty(3, dtype='O')b[:] = [a] * 3

should suffice.


I can't find any elegant solution, but at least a more general solution to doing everything by hand is to declare a function of the form:

def object_array(*args):    array = np.empty(len(args), dtype=np.object)    for i in range(len(args)):        array[i] = args[i]    return array

I can then do:

a = np.array([1,2,3])b = object_array(a,a,a)

I then get:

>>> a = np.array([1,2,3])>>> b = object_array(a,a,a)>>> print b.dtypeobject>>> print b.shape(3,)>>> print b[0].dtypeint64