[AstroPy] Constructing an astropy table with colnames and dtypes of existing table
Erik Bray
embray at stsci.edu
Wed Apr 29 16:20:52 EDT 2015
On 04/29/2015 03:11 PM, Aldcroft, Thomas wrote:
> If anyone is interested in continuing the discussion on github, see:
>
> https://github.com/astropy/astropy/issues/3742
For what it's worth though, I feel like Alexa's original attempt should have
just worked though. If the original dtype is a multi-field dtype (i.e. has a
non-empty .names attribute, which is true for the dtypes on tables), then it
should be sufficient to create a new table with:
new_table = Table(dtype=orig_table.dtype)
it wouldn't even be necessary to explicitly provide a names= argument, since
those can come from the original table's dtype (although if names= is provided
that should override the names in the dtype as long as it's the correct number
of names).
Having empty_like and such sounds fine too, but I don't see why the above
example shouldn't work...
Erik
> On Wed, Apr 29, 2015 at 2:57 PM, Alexa Villaume <avillaum at ucsc.edu
> <mailto: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
> <mailto: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 <mailto:thomas.robitaille at gmail.com>> wrote:
> >
> >> On 29 April 2015 at 19:19, Aldcroft, Thomas
> >> <aldcroft at head.cfa.harvard.edu <mailto:aldcroft at head.cfa.harvard.edu>>
> wrote:
> >>>
> >>>
> >>> On Wed, Apr 29, 2015 at 2:13 PM, Aldcroft, Thomas
> >>> <aldcroft at head.cfa.harvard.edu <mailto:aldcroft at head.cfa.harvard.edu>>
> wrote:
> >>>>
> >>>>
> >>>>
> >>>> On Wed, Apr 29, 2015 at 1:40 PM, Alexa Villaume <avillaum at ucsc.edu
> <mailto: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 <mailto: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 <mailto: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 <mailto: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 <mailto:AstroPy at scipy.org>
> > http://mail.scipy.org/mailman/listinfo/astropy
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org <mailto: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
>
More information about the AstroPy
mailing list