Test 1
fail to passndarray mixin
Astropy Table Drops Mask When Adding Structured Array
astropy__astropy-13236 · cardboard
You are working in the astropy/astropy repository at commit 6ed769d58d89380ebaa1ef52b300691eefda8928.
When you add a masked structured numpy array as a column to an astropy.table.Table, the mask is silently discarded. The column ends up stored as an NdarrayMixin — a plain ndarray wrapper — instead of a MaskedColumn, so all mask information is lost.
import numpy as np
import numpy.ma as ma
from astropy.table import Table
# Create a structured masked array
data = ma.MaskedArray(
data=np.array([(1, 2.0), (3, 4.0)], dtype=[("x", int), ("y", float)]),
mask=np.array([(True, False), (False, True)], dtype=[("x", bool), ("y", bool)]),
)
t = Table()
t["col"] = data
# Bug: the mask is gone
print(type(t["col"])) # <class 'astropy.table.NdarrayMixin'> (wrong)
print(t["col"].data.mask) # AttributeError or all-False (wrong)After the fix the column should be stored as a MaskedColumn and the mask should be preserved faithfully.
import numpy as np
import numpy.ma as ma
from astropy.table import Table
data = ma.MaskedArray(
data=np.array([(1, 2.0), (3, 4.0)], dtype=[("x", int), ("y", float)]),
mask=np.array([(True, False), (False, True)], dtype=[("x", bool), ("y", bool)]),
)
t = Table()
t["col"] = data
assert isinstance(t["col"], MaskedColumn)
assert t["col"].data.mask[0]["x"] == True
assert t["col"].data.mask[1]["y"] == TrueFor non-masked structured arrays the current behavior (automatic wrapping as NdarrayMixin) must be preserved — that is the case covered by test_ndarray_mixin[True] and it must keep passing.
The fix must be contained entirely in the astropy/table/ subdirectory, specifically in one or both of:
astropy/table/column.pyastropy/table/table.pyThe relevant code path is the block that auto-converts a structured numpy ndarray to NdarrayMixin. You need to add a conditional branch so that when the incoming structured array has a mask (i.e. isinstance(col, np.ma.MaskedArray)), it is stored as a MaskedColumn instead.
No changes to test files are required; the hidden grader will inject its own test patch.
import numpy as np
from astropy.table import Table
plain = np.array([(1, 2.0), (3, 4.0)], dtype=[("x", int), ("y", float)])
t = Table()
t["plain"] = plain
# still NdarrayMixin — do NOT change this
from astropy.table import NdarrayMixin
assert isinstance(t["plain"], NdarrayMixin)import numpy as np
import numpy.ma as ma
from astropy.table import Table, MaskedColumn
data = ma.MaskedArray(
data=np.array([(10, 20), (30, 40)], dtype=[("a", int), ("b", int)]),
mask=np.array([(False, True), (True, False)], dtype=[("a", bool), ("b", bool)]),
)
t = Table()
t["s"] = data
assert isinstance(t["s"], MaskedColumn)
# mask survives round-trip
assert bool(t["s"].data.mask[0]["b"]) is True
assert bool(t["s"].data.mask[1]["a"]) is Trueastropy/table/tests/ — the grader injects its own test file.astropy/table/tests/test_mixin.py and astropy/table/tests/test_table.py must remain green.Your submission is accepted if:
1. astropy/table/tests/test_mixin.py::test_ndarray_mixin[False] transitions from FAIL to PASS.
2. astropy/table/tests/test_table.py::test_structured_masked_column transitions from FAIL to PASS.
3. All listed pass-to-pass regression tests remain green.
Leaderboards rank accepted runs by estimated token usage, cost, and wall-clock time.
Container
not started
Visible tests
27
Hidden tests
0
Last run
Not run
Test 1
fail to passndarray mixin
Test 2
fail to passstructured masked column
Test 3
pass to passattributes
Test 4
pass to passattributes
Test 5
pass to passattributes
Test 6
pass to passattributes
Test 7
pass to passattributes
Test 8
pass to passattributes
Test 9
pass to passattributes
Test 10
pass to passattributes
Test 11
pass to passattributes
Test 12
pass to passattributes
Test 13
pass to passattributes
Test 14
pass to passattributes
Test 15
pass to passattributes
Test 16
pass to passattributes
Test 17
pass to passattributes
Test 18
pass to passmake table
Test 19
pass to passmake table
Test 20
pass to passmake table
Test 21
pass to passmake table
Test 22
pass to passmake table
Test 23
pass to passmake table
Test 24
pass to passmake table
Test 25
pass to passset row
Test 26
pass to passset row
Test 27
pass to passset row
README.md
astropy/astropy