Skip to content

Commit

Permalink
BUG: Raise a quieter MaskedArrayFutureWarning for mask changes.
Browse files Browse the repository at this point in the history
Drop the `__getitem__` warning. In `__setitem__` check to see if the
masked array is shared. If it is shared, we know it will propagate
upstream in the future. Also, use a more specific warning type instead
of using `FutureWarning` so that this can be explicitly ignored.
  • Loading branch information
jakirkham committed Feb 8, 2016
1 parent 9524134 commit 594efed
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
MaskType = np.bool_
nomask = MaskType(0)

class MaskedArrayFutureWarning(FutureWarning):
pass


def doc_note(initialdoc, note):
"""
Expand Down Expand Up @@ -3105,13 +3108,6 @@ def __getitem__(self, indx):
Return the item described by i, as a masked array.
"""
# 2016.01.15 -- v1.11.0
warnings.warn(
"Currently, slicing will try to return a view of the data," +
" but will return a copy of the mask. In the future, it will try" +
" to return both as views.",
FutureWarning
)

dout = self.data[indx]
# We could directly use ndarray.__getitem__ on self.
Expand Down Expand Up @@ -3184,13 +3180,17 @@ def __setitem__(self, indx, value):
"""
# 2016.01.15 -- v1.11.0
warnings.warn(
"Currently, slicing will try to return a view of the data," +
" but will return a copy of the mask. In the future, it will try" +
" to return both as views. This means that using `__setitem__`" +
" will propagate values back through all masks that are present.",
FutureWarning
)
self._oldsharedmask = getattr(self, "_oldsharedmask", False)
self._oldsharedmask = self._oldsharedmask or self._sharedmask
if (self._mask is not nomask) and self._oldsharedmask:
warnings.warn(
"Currently, slicing will try to return a view of the data, but"
" will return a copy of the mask. In the future, it will try"
" to return both as views. This means that using"
" `__setitem__` will propagate values back through all masks"
" that are present.",
MaskedArrayFutureWarning
)

if self is masked:
raise MaskError('Cannot alter the masked element.')
Expand Down Expand Up @@ -3234,7 +3234,9 @@ def __setitem__(self, indx, value):
elif not self._hardmask:
# Unshare the mask if necessary to avoid propagation
if not self._isfield:
_oldsharedmask = self._oldsharedmask
self.unshare_mask()
self._oldsharedmask = _oldsharedmask
_mask = self._mask
# Set the data, then the mask
_data[indx] = dval
Expand Down Expand Up @@ -3440,6 +3442,7 @@ def unshare_mask(self):
if self._sharedmask:
self._mask = self._mask.copy()
self._sharedmask = False
self._oldsharedmask = False
return self

sharedmask = property(fget=lambda self: self._sharedmask,
Expand Down

0 comments on commit 594efed

Please sign in to comment.