Promptice

Astropy Table Drops Mask When Adding Structured Array

astropy__astropy-13236 · cardboard

Astropy Table Drops Mask When Adding Structured Array

You are working in the astropy/astropy repository at commit 6ed769d58d89380ebaa1ef52b300691eefda8928.

Problem

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.

Reproduction

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.

Expected Behavior

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"] == True

For 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.

Requirements / Interface

The fix must be contained entirely in the astropy/table/ subdirectory, specifically in one or both of:

  • astropy/table/column.py
  • astropy/table/table.py

The 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.

Examples

Non-masked structured array — behavior unchanged (NdarrayMixin)

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)

Masked structured array — NEW behavior (MaskedColumn)

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 True

Constraints

  • Your patch must not change the behavior for non-masked structured arrays.
  • Do not modify astropy/table/tests/ — the grader injects its own test file.
  • All existing tests in astropy/table/tests/test_mixin.py and astropy/table/tests/test_table.py must remain green.
  • Standard library and numpy/astropy internals only; no new dependencies.
  • Python 3.8+ compatible.

Scoring

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

27 total0 passed0 failed
1

Test 1

fail to pass

ndarray mixin

2

Test 2

fail to pass

structured masked column

3

Test 3

pass to pass

attributes

4

Test 4

pass to pass

attributes

5

Test 5

pass to pass

attributes

6

Test 6

pass to pass

attributes

7

Test 7

pass to pass

attributes

8

Test 8

pass to pass

attributes

9

Test 9

pass to pass

attributes

10

Test 10

pass to pass

attributes

11

Test 11

pass to pass

attributes

12

Test 12

pass to pass

attributes

13

Test 13

pass to pass

attributes

14

Test 14

pass to pass

attributes

15

Test 15

pass to pass

attributes

16

Test 16

pass to pass

attributes

17

Test 17

pass to pass

attributes

18

Test 18

pass to pass

make table

19

Test 19

pass to pass

make table

20

Test 20

pass to pass

make table

21

Test 21

pass to pass

make table

22

Test 22

pass to pass

make table

23

Test 23

pass to pass

make table

24

Test 24

pass to pass

make table

25

Test 25

pass to pass

set row

26

Test 26

pass to pass

set row

27

Test 27

pass to pass

set row

README.md

astropy/astropy

Loading repository...code-server
Loading...
Workspace Terminal