Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual backport of PR 14357 onto v5.0.x (Fix bug creating QTable with units arg set) #14362

Merged
merged 1 commit into from Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.