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

Aldcroft, Thomas aldcroft at head.cfa.harvard.edu
Wed Apr 29 15:11:46 EDT 2015


If anyone is interested in continuing the discussion on github, see:

https://github.com/astropy/astropy/issues/3742

- Tom

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

> Thank you, everyone, for your helpful responses.
>
> If it’s of any interest, I ended up going with Tom A.’s solution because
> one of my columns holds arrays instead of single values. The dtype entry
> for that looks like ‘(‘SPEC’, ‘>f4’, (7925,)’ which was fully copied in Tom
> A.’s solution but not Tom R.’s.
>
> Best,
> Alexa
>
> On Apr 29, 2015, at 11:45 AM, Andrew Hearin <andrew.hearin at yale.edu>
> wrote:
>
> > You beat me to the punch, Tom, I was going to suggest exactly this.
> Numpys built-in empty_like method is extremely convenient, and it would be
> great to have an analogous astropy table method.
> >
> >
> > On Apr 29, 2015, at 2:21 PM, Thomas Robitaille <
> thomas.robitaille at gmail.com> wrote:
> >
> >> On 29 April 2015 at 19:19, Aldcroft, Thomas
> >> <aldcroft at head.cfa.harvard.edu> wrote:
> >>>
> >>>
> >>> On Wed, Apr 29, 2015 at 2:13 PM, Aldcroft, Thomas
> >>> <aldcroft at head.cfa.harvard.edu> wrote:
> >>>>
> >>>>
> >>>>
> >>>> 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.
> >>>
> >>>
> >>> BTW, "all the original table properties" includes all column and table
> meta
> >>> data (e.g. units), so this should be the preferred idiom.
> >>
> >> How about a new class method that does something like
> >> ``np.empty_like`` called ``Table.empty_like``?
> >>
> >> Cheers,
> >> Tom
> >>
> >>>
> >>> - Tom
> >>>
> >>>>
> >>>>
> >>>> In [3]: from astropy.table import Table
> >>>>
> >>>> In [4]: t = Table([[1]])
> >>>>
> >>>> In [5]: t2 = Table(t[0:0])
> >>>>
> >>>> In [6]: t2
> >>>> Out[6]:
> >>>> <Table masked=False length=0>
> >>>> col0
> >>>> int64
> >>>> -----
> >>>>
> >>>> In [7]:
> >>>>
> >>>> - Tom
> >>>>
> >>>>>
> >>>>>
> >>>>> Thank you,
> >>>>> Alexa
> >>>>> _______________________________________________
> >>>>> AstroPy mailing list
> >>>>> AstroPy at scipy.org
> >>>>>
> https://urldefense.proofpoint.com/v2/url?u=http-3A__mail.scipy.org_mailman_listinfo_astropy&d=AwIGaQ&c=-dg2m7zWuuDZ0MUcV7Sdqw&r=AHkQ8HPUDwzl0x62ybAnwN_OEebPRGDtcjUPBcnLYw4&m=tsHzQ6NpihEoX33pmnmiHF6nSNu86BozkIV4v_KCP6s&s=FRlFhyfQHtHEHMGpEAq6bnpkDKvPbatH7Q5WJ492XNU&e=
> >>>>
> >>>>
> >>>
> >>>
> >>> _______________________________________________
> >>> AstroPy mailing list
> >>> AstroPy at scipy.org
> >>>
> https://urldefense.proofpoint.com/v2/url?u=http-3A__mail.scipy.org_mailman_listinfo_astropy&d=AwIGaQ&c=-dg2m7zWuuDZ0MUcV7Sdqw&r=AHkQ8HPUDwzl0x62ybAnwN_OEebPRGDtcjUPBcnLYw4&m=tsHzQ6NpihEoX33pmnmiHF6nSNu86BozkIV4v_KCP6s&s=FRlFhyfQHtHEHMGpEAq6bnpkDKvPbatH7Q5WJ492XNU&e=
> >>>
> >> _______________________________________________
> >> AstroPy mailing list
> >> AstroPy at scipy.org
> >>
> https://urldefense.proofpoint.com/v2/url?u=http-3A__mail.scipy.org_mailman_listinfo_astropy&d=AwIGaQ&c=-dg2m7zWuuDZ0MUcV7Sdqw&r=AHkQ8HPUDwzl0x62ybAnwN_OEebPRGDtcjUPBcnLYw4&m=tsHzQ6NpihEoX33pmnmiHF6nSNu86BozkIV4v_KCP6s&s=FRlFhyfQHtHEHMGpEAq6bnpkDKvPbatH7Q5WJ492XNU&e=
> >
> > _______________________________________________
> > AstroPy mailing list
> > AstroPy at scipy.org
> > http://mail.scipy.org/mailman/listinfo/astropy
>
> _______________________________________________
> 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/ea074f45/attachment.html>


More information about the AstroPy mailing list