From 2f7e491aeeb77fb9d40c9108e57922b876077c3c Mon Sep 17 00:00:00 2001 From: John Kirkham Date: Fri, 15 Jan 2016 16:11:10 -0500 Subject: DEP: Add warnings to `__getitem__` and `__setitem__` to point out the behavior of `MaskedArray`'s masks is changing. --- numpy/ma/core.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 6b1f09f19..690655b36 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -3105,6 +3105,14 @@ class MaskedArray(ndarray): 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. # But then we would have to modify __array_finalize__ to prevent the @@ -3175,6 +3183,15 @@ class MaskedArray(ndarray): locations. """ + # 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 + ) + if self is masked: raise MaskError('Cannot alter the masked element.') _data = self._data -- cgit v1.2.1 From 852eabaa98962d4eb203e1eb2e4d6468cb20ecd0 Mon Sep 17 00:00:00 2001 From: John Kirkham Date: Fri, 15 Jan 2016 16:12:20 -0500 Subject: TEST: Ignore `FutureWarning` if raised from running masked array operations. --- numpy/lib/tests/test_nanfunctions.py | 1 + numpy/ma/tests/test_core.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 7a7b37b98..dafc194eb 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -539,6 +539,7 @@ class TestNanFunctions_Median(TestCase): for axis in [None, 0, 1]: with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') + warnings.simplefilter('ignore', FutureWarning) assert_(np.isnan(np.nanmedian(mat, axis=axis)).all()) if axis is None: assert_(len(w) == 1) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index b163d3b26..d68e63358 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -2223,6 +2223,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) xm[2] = masked x += t(1) @@ -2237,6 +2238,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) m = xm.mask a = arange(10, dtype=t) @@ -2267,6 +2269,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) m = xm.mask a = arange(10, dtype=t) @@ -2297,6 +2300,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) m = xm.mask a = arange(10, dtype=t) @@ -2314,6 +2318,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) x = arange(10, dtype=t) * t(2) xm = arange(10, dtype=t) * t(2) @@ -2330,6 +2335,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) m = xm.mask a = arange(10, dtype=t) @@ -2350,6 +2356,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) x = arange(10, dtype=t) * t(2) xm = arange(10, dtype=t) * t(2) @@ -2385,6 +2392,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): for t in self.othertypes: with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("always") + warnings.simplefilter('ignore', FutureWarning) (x, y, xm) = (_.astype(t) for _ in self.uint8data) m = xm.mask a = arange(10, dtype=t) -- cgit v1.2.1 From 4989360f6bb57e45e1a6f624144117bfd3511313 Mon Sep 17 00:00:00 2001 From: John Kirkham Date: Fri, 15 Jan 2016 16:13:01 -0500 Subject: DOC: Explain that `MaskedArray`s will try to consistently return view of their masks when they are also returning views of their data. --- doc/release/1.11.0-notes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/release/1.11.0-notes.rst b/doc/release/1.11.0-notes.rst index b5d22d770..7f78e387a 100644 --- a/doc/release/1.11.0-notes.rst +++ b/doc/release/1.11.0-notes.rst @@ -20,6 +20,7 @@ Future Changes * Relaxed stride checking will become the default in 1.12.0. * Support for Python 2.6, 3.2, and 3.3 will be dropped in 1.12.0. +* ``MaskedArray``s take views of data **and** masks when slicing in 1.12.0. Compatibility notes -- cgit v1.2.1