8000 Merge pull request #6728 from gerritholl/structured_multidim_masked_a… · numpy/numpy@bdd4558 · GitHub
[go: up one dir, main page]

Skip to content

Commit bdd4558

Browse files
committed
Merge pull request #6728 from gerritholl/structured_multidim_masked_array_fillvalue
BUG/TST: Fix for #6723 including test: force fill_value.ndim==0
2 parents 2df298d + 090e85e commit bdd4558

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

numpy/ma/core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,6 +3129,26 @@ def __getitem__(self, indx):
31293129
if isinstance(indx, basestring):
31303130
if self._fill_value is not None:
31313131
dout._fill_value = self._fill_value[indx]
3132+
3133+
# If we're indexing a multidimensional field in a
3134+
# structured array (such as dtype("(2,)i2,(2,)i1")),
3135+
# dimensionality goes up (M[field].ndim == M.ndim +
3136+
# len(M.dtype[field].shape)). That's fine for
3137+
# M[field] but problematic for M[field].fill_value
3138+
# which should have shape () to avoid breaking several
3139+
# methods. There is no great way out, so set to
3140+
# first element. See issue #6723.
3141+
if dout._fill_value.ndim > 0:
3142+
if not (dout._fill_value ==
3143+
dout._fill_value.flat[0]).all():
3144+
warnings.warn(
3145+
"Upon accessing multidimensional field "
3146+
"{indx:s}, need to keep dimensionality "
3147+
"of fill_value at 0. Discarding "
3148+
"heterogeneous fill_value and setting "
3149+
"all to {fv!s}.".format(indx=indx,
3150+
fv=dout._fill_value[0]))
3151+
dout._fill_value = dout._fill_value.flat[0]
31323152
dout._isfield = True
31333153
# Update the mask if needed
31343154
if _mask is not nomask:

numpy/ma/tests/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,10 @@ def test_fillvalue_exotic_dtype(self):
16621662
assert_equal(test, control)
16631663
control = np.array((0, 0, 0), dtype="int, float, float").astype(ndtype)
16641664
assert_equal(_check_fill_value(0, ndtype), control)
1665+
# but when indexing, fill value should become scalar not tuple
1666+
# See issue #6723
1667+
M = masked_array(control)
1668+
assert_equal(M["f1"].fill_value.ndim, 0)
16651669

16661670
def test_fillvalue_datetime_timedelta(self):
16671671
# Test default fillvalue for datetime64 and timedelta64 types.

0 commit comments

Comments
 (0)
0