[Numpy-discussion] ndarray from column data

Francesc Alted faltet@pytables....
Thu Jul 2 02:58:15 CDT 2009


A Thursday 02 July 2009 03:02:53 Elaine Angelino escrigué:
> Hi there --
>
> Is there a fast way to make a numpy ndarray from column data?
>
> For example, suppose I want to make an ndarray with 2 rows and 3 columns of
> different data types based on the following column data:
>
> C0 = [1,2]
> C1 = ['a','b']
> C2 = [3.3,4.4]
>
> I could create an empty ndarray and fill the columns one by one:
>
> X = numpy.core.ndarray((2,), dtype='<i4,|S1,<f8')
> X['f0'] = C0
> X['f1'] = C1
> X['f2'] = C2
>
> The result is the same as:
>
> X = numpy.array([(1,'a',3.3), (2,'b',4.4)], dtype='<i4,|S1,<f8')
>
> but I would like to make X directly from the column data.
>
> [  I know that numpy.core.records.fromarrays will produce a numpy recarray
> from column data, but this of course is a recarray and not a ndarray! For
> ex:
>
> X = numpy.numpy.core.records.fromarrays([C0,C1,C2])  ]

What's wrong with recarrays?  In any case, if you need a true ndarray object 
you can always do:

ndarr = recarr.view(np.ndarray)

and you are done.

-- 
Francesc Alted


More information about the Numpy-discussion mailing list