[AstroPy] astropy table to numpy array

Aldcroft, Thomas aldcroft at head.cfa.harvard.edu
Thu May 15 21:28:42 EDT 2014


On Thu, May 15, 2014 at 8:01 PM, Derek Homeier <
derek at astro.physik.uni-goettingen.de> wrote:

> On 16.05.2014, at 1:43AM, Perry Greenfield <stsci.perry at gmail.com> wrote:
>
> >> 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.
>
> This should do it in one go (no idea if it's memory-efficient though):
>
> out = np.array([input[c] for c in input.columns if
> input[c].dtype==np.dtype('>f8')])
>
> (you can cut the … if input[c].dtype part if you want to convert your
> integer columns to float).
>
> Does anyone know the difference between columns and colnames BTW?
> I would have expected input.columns to directly give me
> [input[c] for c in input.colnames]
> in the above example, but both seem to return just the names.
>

input.columns is effectively an OrderedDict of the columns, with some added
convenience like being able to select multiple columns by name, integer
index, or with slices.

input.colnames is just a shortcut for list(input.columns.keys()).

- Tom


>
> Cheers,
>                                                 Derek
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20140515/dcc38fc8/attachment.html>


More information about the AstroPy mailing list