Skip to content

table: unable to replace a column by one with a different type #3809

@barentsen

Description

@barentsen

When a column in a table is replaced by one with the same name but a different type, the new type is ignored and only the values are copied. This leads to some unexpected behaviour.

For example:

In [1]: from astropy.table import Table, Column
In [2]: tbl = Table({'col': [1., 2., 3.]})
In [3]: tbl['col'].dtype
Out[3]: dtype('float64')
In [4]: tbl['col'] = tbl['col'].astype('float32')
In [5]: tbl['col'].dtype
Out[5]: dtype('float64')

I would have expected to see dtype('float32') at the end instead.

If the type of the new column is incompatible, the assignment unexpectedly fails altogether:

In [6]: tbl['col'] = Column(['a', 'b', 'c'], name='col')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-26b5b6b20a41> in <module>()
----> 1 tbl['col'] = Column(['a', 'b', 'c'], name='col')

/home/gb/dev/astropy/astropy/table/table.py in __setitem__(self, item, value)
    925             if isinstance(item, six.string_types):
    926                 # Set an existing column
--> 927                 self.columns[item][:] = value
    928 
    929             elif isinstance(item, (int, np.integer)):

/home/gb/dev/astropy/astropy/table/column.py in __setitem__(self, index, value)
    825     # order-of-magnitude speed-up. [#2994]
    826     def __setitem__(self, index, value):
--> 827         self.data[index] = value
    828 
    829     # # Set slices using a view of the underlying data, as it gives an

ValueError: could not convert string to float: 'a'

(I stumbled upon this behaviour by trying to convert magnitudes from doubles to singles before writing a big table into a FITS file.)

The likely fix for the issue is in Table.__setitem__ (i.e. https://github.com/astropy/astropy/blob/master/astropy/table/table.py#L927), but that function looks rather complicated so I'm worried to break stuff -- ping @taldcroft .

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions