diff options
author | Thomas Robitaille <thomas.robitaille@gmail.com> | 2012-11-20 12:17:56 +0100 |
---|---|---|
committer | Thomas Robitaille <thomas.robitaille@gmail.com> | 2012-11-20 12:17:56 +0100 |
commit | 4bacc463602fb7acf3729fb54a5f9797c0038501 (patch) | |
tree | 165c2ee1f34a193e1e5aafe59808acb019ccbd17 /numpy/ma | |
parent | 2a43ed339f1350872207b9871ebdc3fbd0a68d26 (diff) | |
download | numpy-4bacc463602fb7acf3729fb54a5f9797c0038501.tar.gz |
Added test for fill_value behavior in MaskedArray.view, and use fill_value to set the fill value, rather than _fill_value, since using fill_value takes care of setting _fill_value to an array.
Diffstat (limited to 'numpy/ma')
-rw-r--r-- | numpy/ma/core.py | 2 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 32 |
2 files changed, 33 insertions, 1 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index fd49085d6..482959d44 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2897,7 +2897,7 @@ class MaskedArray(ndarray): else: output._fill_value = None else: - output._fill_value = fill_value + output.fill_value = fill_value return output view.__doc__ = ndarray.view.__doc__ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 48ea6e4f9..5920beaa7 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1554,6 +1554,38 @@ class TestFillingValues(TestCase): a = identity(3, fill_value=0., dtype=complex) assert_equal(a.fill_value, 0.) + def test_fillvalue_in_view(self): + "Test the behavior of fill_value in view" + + # Create initial masked array + x = array([1,2,3], fill_value=1, dtype=np.int64) + + # Check that fill_value is preserved by default + y = x.view() + assert_(y.fill_value==1) + + # Check that fill_value is preserved if dtype is specified and the + # dtype has a _fill_value attribute + y = x.view(MaskedArray) + assert_(y.fill_value==1) + print y.fill_value + + # Check that code does not crash if passed a dtype that does not have + # a _fill_value attribute + y = x.view(np.ndarray) + + # Check that fill_value can be overriden with view + y = x.view(MaskedArray, fill_value=2) + assert_(y.fill_value==2) + + # Check that fill_value gets reset if passed a dtype but not a + # fill_value. This is because even though in some cases one can safely + # cast the fill_value, e.g. if taking an int64 view of an int32 array, + # in other cases, this cannot be done (e.g. int32 view of an int64 + # array with a large fill_value). + y = x.view(np.int32) + assert_(y.fill_value == 999999) + #------------------------------------------------------------------------------ class TestUfuncs(TestCase): |