mtc*_*mtc 5 python arrays numpy fits
在之前的帖子中,我已经看到可以使用 来更改dtypea 。但是,我无法设法使用其中一列中有一个数组的数组来做到这一点。recarrayastyperecarray
我的recarray来自 FITS 文件记录:
> f = fits.open('myfile.fits')
> tbdata = f[1].data
> tbdata
# FITS_rec([ (0.27591679999999996, array([570, 576, 566, ..., 571, 571, 569], dtype=int16)),
# (0.55175680000000005, array([575, 563, 565, ..., 572, 577, 582], dtype=int16)),
# ...,
# (2999.2083967999997, array([574, 570, 575, ..., 560, 551, 555], dtype=int16)),
# (2999.4842367999995, array([575, 583, 578, ..., 559, 565, 568], dtype=int16)],
# dtype=[('TIME', '>f8'), ('AC', '>i4', (2,))])
Run Code Online (Sandbox Code Playgroud)
我需要将AC列转换为 ,int所以float我尝试过:
> tbdata = tbdata.astype([('TIME', '>f8'), ('AC', '>f4', (2,))])
Run Code Online (Sandbox Code Playgroud)
而且,尽管看起来dtype确实发生了变化
> tbdata.dtype
# dtype([('TIME', '>f8'), ('AC', '>f4', (2,))])
Run Code Online (Sandbox Code Playgroud)
查看AC中的数据表明它们仍然是整数值。例如,sum计算达到变量的极限int16(所有AC列值为正):
> tbdata['AC'][0:55].sum()
# _VLF(array([31112, 31128, 31164, ..., 31203, 31232, 31262], dtype=int16), dtype=object)
> tbdata['AC'][0:65].sum()
# _VLF(array([-28766, -28759, -28702, ..., -28659, -28638, -28583], dtype=int16), dtype=object)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以有效改变数组数据类型吗?
我可以用一个合适的文件重现这个问题recarray。解决方法是将其加载recarray为拟合表,然后将其转换为 pandas 数据框:
from astropy.table import Table
import pandas as pd
t = Table.read('file.fits')
df = pd.DataFrame.from_records(t, columns=t.columns)
df.AC = df.AC.astype(float)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
778 次 |
| 最近记录: |