[AstroPy] Changing table column from float64 to float32

Homeier, Derek dhomeie at gwdg.de
Mon Jul 11 20:13:21 EDT 2022


On 11 Jul 2022, at 11:14 pm, Michael Brewer <brewer at astro.umass.edu<mailto:brewer at astro.umass.edu>> wrote:

Please try this:

table_hdu.replace_column('a', table_hdu.data['a'].astype(np.single))

Michael

replace_column is a [astropy.table.]Table method; this won’t work on a FITS BinTable –
I think the most direct way is indeed to recreate the HDU from the appropriately cast
Columns as outlined by Peter. Note that the HDU also contains the corresponding header
information characterising the data section formats, so direct manipulation of the data
parts is limited for a reason.

On 7/11/22 12:17 PM, Peter Weilbacher wrote:
Hi Ivan,

I have no particular insight into this, but it seems to me that since
you created the Column objects with a specific format, assigning any
data to it will cause it to be casted to match that. (Otherwise the
underlying memory buffer does not match.)

Instead, I guess you could create a new column with the desired type and
format and recreate the table using that:

c1_single = fits.Column(name='a', array=table_hdu.data['a'].astype(np.single), format='E')
table_hdu2 = fits.BinTableHDU.from_columns(c1_single + table_hdu.columns[1:])

(It appears that you can even remove the .astype() from this and it
still works.)

Yes, the `format=‘E’` specifier will already recast to the respective FITS (= Fortran) format.

Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/astropy/attachments/20220712/8996f44d/attachment.html>


More information about the AstroPy mailing list