Skip to content

Commit d4b1eac

Browse files
authored
Merge pull request #23634 from charris/backport-23627
BUG: Ignore invalid and overflow warnings in masked setitem
2 parents 4e60648 + 06c2694 commit d4b1eac

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

numpy/ma/core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,6 +3328,10 @@ def _scalar_heuristic(arr, elem):
33283328
# Note: Don't try to check for m.any(), that'll take too long
33293329
return dout
33303330

3331+
# setitem may put NaNs into integer arrays or occasionally overflow a
3332+
# float. But this may happen in masked values, so avoid otherwise
3333+
# correct warnings (as is typical also in masked calculations).
3334+
@np.errstate(over='ignore', invalid='ignore')
33313335
def __setitem__(self, indx, value):
33323336
"""
33333337
x.__setitem__(i, y) <==> x[i]=y
@@ -4619,6 +4623,7 @@ def ravel(self, order='C'):
46194623
otherwise. 'K' means to read the elements in the order they occur
46204624
in memory, except for reversing the data when strides are negative.
46214625
By default, 'C' index order is used.
4626+
(Masked arrays currently use 'A' on the data when 'K' is passed.)
46224627
46234628
Returns
46244629
-------
@@ -4645,6 +4650,13 @@ def ravel(self, order='C'):
46454650
fill_value=999999)
46464651
46474652
"""
4653+
# The order of _data and _mask could be different (it shouldn't be
4654+
# normally). Passing order `K` or `A` would be incorrect.
4655+
# So we ignore the mask memory order.
4656+
# TODO: We don't actually support K, so use A instead. We could
4657+
# try to guess this correct by sorting strides or deprecate.
4658+
if order in "kKaA":
4659+
order = "C" if self._data.flags.fnc else "F"
46484660
r = ndarray.ravel(self._data, order=order).view(type(self))
46494661
r._update_from(self)
46504662
if self._mask is not nomask:

numpy/ma/tests/test_core.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,24 @@ def test_indexing(self):
374374
assert_equal(s1, s2)
375375
assert_(x1[1:1].shape == (0,))
376376

377+
def test_setitem_no_warning(self):
378+
# Setitem shouldn't warn, because the assignment might be masked
379+
# and warning for a masked assignment is weird (see gh-23000)
380+
# (When the value is masked, otherwise a warning would be acceptable
381+
# but is not given currently.)
382+
x = np.ma.arange(60).reshape((6, 10))
383+
index = (slice(1, 5, 2), [7, 5])
384+
value = np.ma.masked_all((2, 2))
385+
value._data[...] = np.inf # not a valid integer...
386+
x[index] = value
387+
# The masked scalar is special cased, but test anyway (it's NaN):
388+
x[...] = np.ma.masked
389+
# Finally, a large value that cannot be cast to the float32 `x`
390+
x = np.ma.arange(3., dtype=np.float32)
391+
value = np.ma.array([2e234, 1, 1], mask=[True, False, False])
392+
x[...] = value
393+
x[[0, 1, 2]] = value
394+
377395
@suppress_copy_mask_on_assignment
378396
def test_copy(self):
379397
# Tests of some subtle points of copying and sizing.

0 commit comments

Comments
 (0)