Skip to content

Commit

Permalink
Ensure names of mixin columns are propagated
Browse files Browse the repository at this point in the history
  • Loading branch information
mhvk committed Jan 10, 2024
1 parent 3381354 commit 36a54ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions astropy/io/ascii/mrt.py
Expand Up @@ -599,10 +599,10 @@ def write(self, lines):
# 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 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.

0 comments on commit 36a54ab

Please sign in to comment.