How to change dtype numpy recarray when one of the columns is an array?

In previous posts, I saw that a change dtypein recarraycan be done with astype. However, I cannot do this with help recarraythat has an array in one of its columns.

My recarraycomes from the FITS file entry:

> 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,))])

I need to convert the AC column from intto float, so I tried:

> tbdata = tbdata.astype([('TIME', '>f8'), ('AC', '>f4', (2,))])

and although it seems that dtypereally changed

> tbdata.dtype
# dtype([('TIME', '>f8'), ('AC', '>f4', (2,))])

A look at the data in AC shows that they are still integer values. For example, the calculation sumreaches the limits of the variable int16(all values ​​of the AC column are positive):

> 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)

Is there a way to effectively change the data type of an array?

+4
2

recarray fits. recarray , pandas dataframe:

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)
0

, recarray "", :

> ra = np.array([ ([30000,10000], 1), ([30000,20000],2),([30000,30000],3) ], dtype=[('x', 'int16',2), ('y', int)])
> ra
# array([([30000, 10000], 1), ([30000, 20000], 2), ([30000, 30000], 3)],
#       dtype=[('x', '<i2', (2,)), ('y', '<i8')])
> ra = ra.astype([('x', '<f4', (2,)), ('y', '<i8')])
> ra
# array([([30000.0, 10000.0], 1), ([30000.0, 20000.0], 2),
#        ([30000.0, 30000.0], 3)], dtype=[('x', '<f4', (2,)), ('y', '<i8')])

, int16 numbers .

astype tbdata recarray ( dtype):

> tbdata.dtype
# dtype([('TIME', '>f8'), ('AC', '>f4', (2,))])
> 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'), ('ADC', '<f4', (2,))])

, , AstroPy FITS. , , sum(), , tbdata, - , FITS 32768, TZERO . , CFITSIO FITS , . .

0

Source: https://habr.com/ru/post/1620577/


All Articles