Skip to content

Commit

Permalink
Merge pull request #14362 from pllim/backport-of-pr14357-to-v5.0.x
Browse files Browse the repository at this point in the history
Manual backport of PR 14357 onto v5.0.x (Fix bug creating QTable with units arg set)
  • Loading branch information
pllim committed Feb 6, 2023
2 parents 92d473b + f8b9cbd commit 57a9c5d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion astropy/table/table.py
Expand Up @@ -922,7 +922,12 @@ def _set_column_attribute(self, attr, values):
value = None

if value not in (np.ma.masked, None):
setattr(self[name].info, attr, value)
col = self[name]
if attr == "unit" and isinstance(col, Quantity):
# Update the Quantity unit in-place
col <<= value
else:
setattr(col.info, attr, value)

def __getstate__(self):
columns = OrderedDict(
Expand Down
15 changes: 14 additions & 1 deletion astropy/table/tests/test_init_table.py
Expand Up @@ -6,8 +6,8 @@
import pytest
import numpy as np

from astropy.table import Column, TableColumns, Table, MaskedColumn
import astropy.units as u
from astropy.table import Column, MaskedColumn, QTable, Table, TableColumns


class DictLike(Mapping):
Expand Down Expand Up @@ -619,3 +619,16 @@ def test_init_Table_from_list_of_quantity():
assert np.all(t["x"] == [5, 10])
assert t["y"][0] == 1 * u.m
assert t["y"][1] == 3


def test_init_QTable_and_set_units():
"""
Test fix for #14336 where providing units to QTable init fails.
This applies when the input is a Quantity.
"""
t = QTable([[1, 2] * u.km, [1, 2]], units={"col0": u.m, "col1": u.s})
assert t["col0"].unit == u.m
assert np.all(t["col0"].value == [1000, 2000])
assert t["col1"].unit == u.s
assert np.all(t["col1"].value == [1, 2])
3 changes: 3 additions & 0 deletions docs/changes/table/14357.bugfix.rst
@@ -0,0 +1,3 @@
Fix a bug when creating a ``QTable`` when a ``Quantity`` input column is present and the
``units`` argument modifies the unit of that column. This now works as expected where
previously this caused an exception.

0 comments on commit 57a9c5d

Please sign in to comment.