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

Thomas Robitaille thomas.robitaille at gmail.com
Wed Apr 29 14:08:25 EDT 2015


Hi Alexa,

First, I think it might make sense for us to fix Table to allow Numpy
dtypes to be passed to dtype= in Table (the only subtlety is that if a
dtype with field names is passed in addition to names= we need to decide
which takes precedence). Maybe Tom Aldcroft can comment on this?

In the mean time I would suggest the easiest way to do this is to
separate the field names from the types using the following:

names, types = zip(*irtf.dtype.descr)

then you can pass 'types' to dtype in the Table constructor. This will
create an empty table with no rows (as opposed to Andrew's example which
will create a table with the same number of rows).

Cheers,
Tom

Alexa Villaume 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?
> 
> Thank you,
> Alexa
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy



More information about the AstroPy mailing list