Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions astropy/table/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,8 +1249,17 @@ def __setitem__(self, item, value):
n_cols = len(self.columns)

if isinstance(item, six.string_types):
# Set an existing column
self.columns[item][:] = value
# Set *in-place* an existing column
col = self.columns[item]
col[:] = value

# Transfer attributes when setting to a Column instance.
# TODO later: generalize to mixin columns on the right side (value)
if isinstance(value, Column):
for attr in ('description', 'unit', 'format', 'meta'):
setattr(col.info, attr, getattr(value, attr))
# Note that setattr(col.info, ...) is used instead of setattr(col, ..)
# because this generalizes to mixins (on the left side).

elif isinstance(item, (int, np.integer)):
# Set the corresponding row assuming value is an iterable.
Expand Down
15 changes: 15 additions & 0 deletions astropy/table/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1747,3 +1747,18 @@ def test_qtable_read_for_ipac_table_with_char_columns():
t1.write(out, format="ascii.ipac")
t2 = table.QTable.read(out.getvalue(), format="ascii.ipac", guess=False)
assert t2["B"].unit is None


def test_table_setitem_column():
"""
Test that setitem with a `Column` works properly.

Regression test for https://github.com/astropy/astropy/issues/2630
"""
t = table.Table()
t['a'] = table.Column([1, 2, 3], unit='m', description='old')
t['a'] = table.Column([4, 5, 6], unit='s', description='new')

assert_allclose(t['a'].data, [4, 5, 6])
assert t['a'].unit == u.Unit('s')
assert t['a'].description == 'new'