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

Ensure names of Quantity and other mixin columns are propagated #15848

Merged
merged 2 commits into from Jan 10, 2024
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
4 changes: 2 additions & 2 deletions astropy/io/ascii/mrt.py
Expand Up @@ -599,10 +599,10 @@
# Convert all other ``mixin`` columns to ``Column`` objects.
# Parsing these may still lead to errors!
elif not isinstance(col, Column):
col = Column(col)
col = Column(col, name=self.colnames[i])

Check warning on line 602 in astropy/io/ascii/mrt.py

View check run for this annotation

Codecov / codecov/patch

astropy/io/ascii/mrt.py#L602

Added line #L602 was not covered by tests
# If column values are ``object`` types, convert them to string.
if np.issubdtype(col.dtype, np.dtype(object).type):
col = Column([str(val) for val in col])
col = Column([str(val) for val in col], name=col.name)

Check warning on line 605 in astropy/io/ascii/mrt.py

View check run for this annotation

Codecov / codecov/patch

astropy/io/ascii/mrt.py#L605

Added line #L605 was not covered by tests
self.cols[i] = col

# Delete original ``SkyCoord`` columns, if there were any.
Expand Down
26 changes: 19 additions & 7 deletions astropy/io/ascii/tests/test_cds.py
Expand Up @@ -13,7 +13,7 @@
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.io import ascii
from astropy.table import Column, MaskedColumn, Table
from astropy.table import Column, MaskedColumn, QTable, Table
from astropy.time import Time
from astropy.utils.data import get_pkg_data_filename
from astropy.utils.exceptions import AstropyWarning
Expand Down Expand Up @@ -427,16 +427,17 @@ def test_write_mixin_and_broken_cols():
"--------------------------------------------------------------------------------",
" Bytes Format Units Label Explanations",
"--------------------------------------------------------------------------------",
" 1- 7 A7 --- name Description of name ",
" 9- 74 A66 --- Unknown Description of Unknown",
" 76-114 A39 --- Unknown Description of Unknown",
"116-138 A23 --- Unknown Description of Unknown",
" 1- 7 A7 --- name Description of name ",
" 9- 74 A66 --- Unknown Description of Unknown ",
" 76-114 A39 --- cart Description of cart ",
"116-138 A23 --- time Description of time ",
"140-142 F3.1 m q [1.0/1.0] Description of q",
"--------------------------------------------------------------------------------",
"Notes:",
"--------------------------------------------------------------------------------",
"HD81809 <SkyCoord (ICRS): (ra, dec) in deg",
" (330.564375, -61.65961111)> (0.41342785, -0.23329341, -0.88014294) 2019-01-01 00:00:00.000",
"random 12 (0.41342785, -0.23329341, -0.88014294) 2019-01-01 00:00:00.000",
" (330.564375, -61.65961111)> (0.41342785, -0.23329341, -0.88014294) 2019-01-01 00:00:00.000 1.0",
"random 12 (0.41342785, -0.23329341, -0.88014294) 2019-01-01 00:00:00.000 1.0",
]
t = Table()
t["name"] = ["HD81809"]
Expand All @@ -445,6 +446,7 @@ def test_write_mixin_and_broken_cols():
t.add_row(["random", 12])
t["cart"] = coord.cartesian
t["time"] = Time("2019-1-1")
t["q"] = u.Quantity(1.0, u.m)
out = StringIO()
t.write(out, format="ascii.mrt")
lines = out.getvalue().splitlines()
Expand Down Expand Up @@ -548,3 +550,13 @@ def test_write_skycoord_with_format():
lines = lines[i_bbb:] # Select Byte-By-Byte section and following lines.
# Check the written table.
assert lines == exp_output


def test_write_qtable():
# Regression test for gh-12804
qt = QTable([np.arange(4) * u.m, ["a", "b", "c", "ddd"]], names=["a", "b"])
out = StringIO()
qt.write(out, format="mrt")
result = out.getvalue()
assert "Description of a" in result
assert "Description of b" in result
2 changes: 2 additions & 0 deletions astropy/table/column.py
Expand Up @@ -553,6 +553,8 @@
format = data.info.format
if meta is None:
meta = data.info.meta
if name is None:
name = data.info.name

Check warning on line 557 in astropy/table/column.py

View check run for this annotation

Codecov / codecov/patch

astropy/table/column.py#L556-L557

Added lines #L556 - L557 were not covered by tests

else:
if np.dtype(dtype).char == "S":
Expand Down
13 changes: 13 additions & 0 deletions astropy/table/tests/test_column.py
Expand Up @@ -156,6 +156,19 @@ def test_quantity_init(self, Column):
assert np.all(c.data == np.array([100, 200, 300]))
assert np.all(c.unit == u.cm)

def test_quantity_with_info_init(self, Column):
q = np.arange(3.0) * u.m
q.info.name = "q"
q.info.description = "an example"
q.info.meta = {"parrot": "dead"}
q.info.format = "3.1f"
c = Column(q)
assert c.name == "q"
assert c.description == "an example"
assert c.meta == q.info.meta
assert c.meta is not q.info.meta
assert c.pformat() == " q \n---\n0.0\n1.0\n2.0".splitlines()

def test_quantity_comparison(self, Column):
# regression test for gh-6532
c = Column([1, 2100, 3], unit="Hz")
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/io.ascii/15848.bugfix.rst
@@ -0,0 +1,2 @@
Ensure that the names of mixin columns are properly propagated as
labels for the MRT format.
2 changes: 2 additions & 0 deletions docs/changes/table/15848.bugfix.rst
@@ -0,0 +1,2 @@
Ensure that if a ``Column`` is initialized with a ``Quantity`` it will use by
default a possible name defined on the quantity's ``.info``.