[AstroPy] astropy table to numpy array

Erik Bray embray at stsci.edu
Fri May 16 11:01:01 EDT 2014


On 05/15/2014 07:43 PM, Perry Greenfield wrote:
>
> On May 15, 2014, at 7:20 PM, martin wrote:
>
>> Hello,
>> I’m trying to convert an astropy.table.table.Table object (named out) to a numpy array, but it will convert each row to a tuple.
>> Also the type for the array is strange. I copy a snippet of what I’m trying to do below:
>>
>> np.array(out)
>> Out[77]:
>> array([(1, 9642.374, 41.886, 1.109, 2.45, 0.718),
>>       (2, 1430.236, 57.642, 1.277, 2.33, 0.51),
>>       (3, 638.677, 77.257, 1.254, 5.18, 0.784), ...,
>>       (6762, 1211.064, 937.82, 907.38, 1.81, 0.25),
>>       (6763, 1090.107, 911.179, 906.314, 2.68, 0.353),
>>       (6764, 2348.992, 333.94, 899.355, 3.67, 0.591)],
>>      dtype=[('NUMBER', '<i8'), ('FLUX_APER', '<f8'), ('X_IMAGE', '<f8'), ('Y_IMAGE', '<f8'), ('FWHM_IMAGE', '<f8'), ('ELLIPTICITY', '<f8')])
>>
>>
>> Does anyone know how to get a 2D numpy array of all floats from an astropy table?
>
> Actually it is a numpy array, just not a simple one. It's a array of records or structs.
>
> You'll first have to realize that nothing  can do that generally, particularly if some of the columns of the table aren't floats, and particularly if they aren't numbers at all. In this case they aren't all floats, but at least they are numbers.
>
> You could do it the safest way by inserting each column into the appropriately sized numpy array one column at a time in a loop (what I would recommend unless someone knows a better solution). One can essentially equivalence all but the first column to a 2d float array, but that is a bit tricky and unless you need the performance, I wouldn't recommend it.
>
> Something like (I haven't tested this!)
>
> out = np.zeros((len(input),ncolumns),dtype=np.float64)
> for n, (columnname, dummy) in enumerate(input.dtype):
> 	out[:,n] = input[columnname].astype(np.float64)

In general one would have to do something like this.  However if one can be 
*certain* that for a particular table all the columns are float64 (or otherwise 
the same type--this could be checked in advance) then it would still be possible 
to do something as simply as:

data.view(np.float64).reshape(len(data), len(data.dtype))

where for a record array len(data) is the number of rows, and len(data.dtype) 
will give you the number of columns.

Erik



More information about the AstroPy mailing list