diff options
| author | Charles Harris <charlesr.harris@gmail.com> | 2023-04-21 18:14:41 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-21 18:14:41 -0400 |
| commit | 5fd25ab98c5834c6806564dfb2d937bd48933940 (patch) | |
| tree | 4579b007087b92435d613413d2f85264685df9a3 /numpy | |
| parent | afa98ddefde2af25676ae52837d648ee7d2b6e93 (diff) | |
| parent | 0655cb9e24cff97642c70a98c5258bcbe7a6c3d3 (diff) | |
| download | numpy-5fd25ab98c5834c6806564dfb2d937bd48933940.tar.gz | |
Merge pull request #23627 from seberg/issue-23000
BUG: Ignore invalid and overflow warnings in masked setitem
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/ma/core.py | 12 | ||||
| -rw-r--r-- | numpy/ma/tests/test_core.py | 18 |
2 files changed, 30 insertions, 0 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index d2bd3d47e..0cf748dfd 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3340,6 +3340,10 @@ class MaskedArray(ndarray): # Note: Don't try to check for m.any(), that'll take too long return dout + # setitem may put NaNs into integer arrays or occasionally overflow a + # float. But this may happen in masked values, so avoid otherwise + # correct warnings (as is typical also in masked calculations). + @np.errstate(over='ignore', invalid='ignore') def __setitem__(self, indx, value): """ x.__setitem__(i, y) <==> x[i]=y @@ -4631,6 +4635,7 @@ class MaskedArray(ndarray): otherwise. 'K' means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, 'C' index order is used. + (Masked arrays currently use 'A' on the data when 'K' is passed.) Returns ------- @@ -4657,6 +4662,13 @@ class MaskedArray(ndarray): fill_value=999999) """ + # The order of _data and _mask could be different (it shouldn't be + # normally). Passing order `K` or `A` would be incorrect. + # So we ignore the mask memory order. + # TODO: We don't actually support K, so use A instead. We could + # try to guess this correct by sorting strides or deprecate. + if order in "kKaA": + order = "C" if self._data.flags.fnc else "F" r = ndarray.ravel(self._data, order=order).view(type(self)) r._update_from(self) if self._mask is not nomask: diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 5db01b74a..1f8101559 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -377,6 +377,24 @@ class TestMaskedArray: assert_equal(s1, s2) assert_(x1[1:1].shape == (0,)) + def test_setitem_no_warning(self): + # Setitem shouldn't warn, because the assignment might be masked + # and warning for a masked assignment is weird (see gh-23000) + # (When the value is masked, otherwise a warning would be acceptable + # but is not given currently.) + x = np.ma.arange(60).reshape((6, 10)) + index = (slice(1, 5, 2), [7, 5]) + value = np.ma.masked_all((2, 2)) + value._data[...] = np.inf # not a valid integer... + x[index] = value + # The masked scalar is special cased, but test anyway (it's NaN): + x[...] = np.ma.masked + # Finally, a large value that cannot be cast to the float32 `x` + x = np.ma.arange(3., dtype=np.float32) + value = np.ma.array([2e234, 1, 1], mask=[True, False, False]) + x[...] = value + x[[0, 1, 2]] = value + @suppress_copy_mask_on_assignment def test_copy(self): # Tests of some subtle points of copying and sizing. |
