diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/ma/core.py | 12 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 24 |
2 files changed, 30 insertions, 6 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 85a830661..9259aa4c7 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3008,11 +3008,13 @@ class MaskedArray(ndarray): except (TypeError, AttributeError): # When _mask.shape is not writable (because it's a void) pass - # Finalize the fill_value for structured arrays - if self.dtype.names is not None: - if self._fill_value is None: - self._fill_value = _check_fill_value(None, self.dtype) - return + + # Finalize the fill_value + if self._fill_value is not None: + self._fill_value = _check_fill_value(self._fill_value, self.dtype) + elif self.dtype.names is not None: + # Finalize the default fill_value for structured arrays + self._fill_value = _check_fill_value(None, self.dtype) def __array_wrap__(self, obj, context=None): """ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index aa7672daa..50ab6e1de 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4985,7 +4985,7 @@ def test_ufunc_with_out_varied(): assert_equal(res_pos.data, expected.data) -def test_astype(): +def test_astype_mask_ordering(): descr = [('v', int, 3), ('x', [('y', float)])] x = array([ [([1, 2, 3], (1.0,)), ([1, 2, 3], (2.0,))], @@ -5017,6 +5017,28 @@ def test_astype(): assert_(x_f2.mask.flags.f_contiguous) +dts = [np.dtype(dt_) for dt_ in '?bhilqBHILQefdgFD'] +ids = [dt_.char for dt_ in dts] + +@pytest.mark.parametrize('dt1', dts, ids=ids) +@pytest.mark.parametrize('dt2', dts, ids=ids) +@pytest.mark.filterwarnings('ignore::numpy.ComplexWarning') +def test_astype_basic(dt1, dt2): + # See gh-12070 + src = np.ma.array(ones(3, dt1), fill_value=1) + dst = src.astype(dt2) + + assert_(src.fill_value == 1) + assert_(src.dtype == dt1) + assert_(src.fill_value.dtype == dt1) + + assert_(dst.fill_value == 1) + assert_(dst.dtype == dt2) + assert_(dst.fill_value.dtype == dt2) + + assert_equal(src, dst) + + def test_fieldless_void(): dt = np.dtype([]) # a void dtype with no fields x = np.empty(4, dt) |