[AstroPy] Constructing an astropy table with colnames and dtypes of existing table

Aldcroft, Thomas aldcroft at head.cfa.harvard.edu
Wed Apr 29 14:13:26 EDT 2015

On Wed, Apr 29, 2015 at 1:40 PM, Alexa Villaume <avillaum at ucsc.edu> wrote:

> Hi everybody,
> I’m trying to construct an astropy table using the exact same columns and
> corresponding dtypes of an existing table. The documentation goes into
> using existing columns but it wasn’t as clear to me on how to handle the
> data types. For example, I have an existing table ‘irtf’ and I want to make
> a table ‘irtf_hb’ if I do,
> irtf_hb = Table(names=irtf.colnames)
> the table is initialized with all the right column names but the dtypes
> are all floats. However, I can’t do this,
> irtf_hb = Table(names=irtf.colnames, dtype=irtf.dtype)
> Because I get an error, “ValueError: dtype must be a list or None”. I
> haven’t had any luck with trying to change the type of irtf.dtype or using
> list comprehension to extract a list of dtypes.
> Is there a simple way to use the dtypes of an existing table to initialize
> a new table?

You should be able to do:

>>> irtf_hb = Table(irtf[0:0])

This is a slight hack, but it's pretty simple and should give you a
zero-length table with all the original table properties.  I tested on a
simple case but haven't checked in detail.

In [3]: from astropy.table import Table

In [4]: t = Table([[1]])

In [5]: t2 = Table(t[0:0])

In [6]: t2
<Table masked=False length=0>

In [7]:

- Tom

> Thank you,
> Alexa
> _______________________________________________
> 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/20150429/402118f3/attachment.html>

More information about the AstroPy mailing list