Skip to content

QTable converts numbers to float, if unit is unknown #15447

Closed

Description

@hamogu

Description

When loading a fits file with a numeric column in which is formatted as an int with an associated unit that QTable does not recognize, the column is changed to a float.

Expected behavior

Report a warning about the unit, but do not change the dtype of the data (this is what happens when I read with Table).

How to Reproduce

>>> from astropy.table import Table, QTable
>>> t = Table({'a':[1,2,3], 'b': [4., 5., 6.]})
>>> t['a'].unit = 'channel'
>>> t.write('test.fits', format='fits', overwrite=True)
WARNING: UnitsWarning: 'channel' did not parse as fits unit: At col 0, Unit 'channel' not supported by the FITS standard.  If this is meant to be a custom unit, define it with 'u.def_unit'. To have it recognized inside a file reader or other code, enable it with 'u.add_enabled_units'. For details, see https://docs.astropy.org/en/latest/units/combining_and_defining.html [astropy.units.core]
>>> tab = Table.read('test.fits')
>>> tab
<Table length=3>
   a       b   
channel        
 int64  float64
------- -------
      1     4.0
      2     5.0
      3     6.0
>>> tab = QTable.read('test.fits')
>>> tab
<QTable length=3>
   a       b   
channel        
float64 float64
------- -------
    1.0     4.0
    2.0     5.0
    3.0     6.0

Versions

macOS-13.2.1-arm64-arm-64bit
Python 3.9.9 | packaged by conda-forge | (main, Dec 20 2021, 02:41:07)
[Clang 11.1.0 ]
astropy 6.0.dev776+g40e6ed90d2
Numpy 1.22.0
pyerfa 2.0.0.1
Scipy 1.7.3
Matplotlib 3.5.1

Activity

  1. pllim commented on Oct 5, 2023

    @pllim
    Member

    Whether the unit is valid or not doesn't matter. Quantity always cast the values to float. I get the same behavior even if I set the unit to meters.

    >>> t['a'].unit = 'm'
    >>> QTable.read('test.fits')
    <QTable length=3>
       a       b
       m
    float64 float64
    ------- -------
        1.0     4.0
        2.0     5.0
        3.0     6.0

    I think this behavior was reported elsewhere (even when QTable is not involved) but I cannot find the issue now, maybe @mhvk remembers.

  2. mhvk commented on Oct 12, 2023

    @mhvk
    Contributor

    There is tentative PR for fixing this at #12505. Note that there was quite a bit of discussion of exactly when one should upcast, but I think it was most of the way there...

  3. taldcroft commented on Sep 23, 2026

    @taldcroft
    Member

    Fixed by #20273.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions