如何将 .fits 文件/astropy table 转换为 pandas df 并反转?

How to convert .fits file/ astropy table to pandas df and reverse?

我需要经常将 pandas df 转换为 .fits 文件并进行反向转换 - 我得到了 .fits 文件,我更容易将它们处理为 pandas dfs。在这些文件类型之间进行转换的最直接方法是什么?

# 1.)  how to convert a pandas df 
# (or almost any ascii data file) to astropy table
# and save the table as .fits file

from astropy.table import Table
import pandas as pd

df = pd.read_csv('some_file.dat', delim_whitespace=True, skiprows=7, names=['A', 'B', 'C'])

t = Table.from_pandas(df)
t.write('some_file.fits')




# 2.)  how to convert a fits file (or astropy table) to pandas df

dat = Table.read('some_file.fits')
df = dat.to_pandas()



# 3.)  it is like 1.) but without a df as intermediate step - 
# how to read a csv/ascii file directly as astropy table
# and save it as .fits   

table = Table.read('some_file.dat', format='ascii.tab')
table.write('some_file.fits', overwrite=True)