diff options
author | pierregm <pierregm@localhost> | 2008-08-04 18:05:11 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2008-08-04 18:05:11 +0000 |
commit | 84dbd03a91eac58006fc5d4bd9d10f23c6a78ca0 (patch) | |
tree | 3b9117659e2c2d900668c71e99c2fb961a45f323 | |
parent | ce5fc0af6285ca4ed1478c42c48fd1f6a7909f83 (diff) | |
download | numpy-84dbd03a91eac58006fc5d4bd9d10f23c6a78ca0.tar.gz |
core
MaskedArray.__new__: prevents data.mask to change shape and force a copy of _data.mask
-rw-r--r-- | numpy/ma/core.py | 6 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 8 |
2 files changed, 14 insertions, 0 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index b4f08171a..c8664c754 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1229,6 +1229,9 @@ class MaskedArray(ndarray): _data = np.array(data, dtype=dtype, copy=copy, subok=True, ndmin=ndmin) _baseclass = getattr(data, '_baseclass', type(_data)) _optinfo = {} + # Check that we'ew not erasing the mask.......... + if isinstance(data,MaskedArray) and (data.shape != _data.shape): + copy = True # Careful, cls might not always be MaskedArray... if not isinstance(data, cls) or not subok: _data = _data.view(cls) @@ -1267,6 +1270,9 @@ class MaskedArray(ndarray): if copy: _data._mask = _data._mask.copy() _data._sharedmask = False + # Reset the shape of the original mask + if getmask(data) is not nomask: + data._mask.shape = data.shape else: _data._sharedmask = True # Case 2. : With a mask in input ........ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index e581e274e..e24fa6f54 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -139,6 +139,14 @@ class TestMaskedArray(TestCase): assert_equal(x._data,[[1,2,3]]) assert_equal(x._mask,[[1,0,0]]) + def test_creation_ndmin_from_maskedarray(self): + "Make sure we're not losing the original mask w/ ndmin" + x = array([1,2,3]) + x[-1] = masked + xx = array(x, ndmin=2, dtype=float) + assert_equal(x.shape, x._mask.shape) + assert_equal(xx.shape, xx._mask.shape) + def test_creation_maskcreation(self): "Tests how masks are initialized at the creation of Maskedarrays." data = arange(24, dtype=float) |