Permalink
Browse files

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.
  • Loading branch information...
1 parent 2a43ed3 commit 4bacc463602fb7acf3729fb54a5f9797c0038501 @astrofrog astrofrog committed Nov 20, 2012
Showing with 33 additions and 1 deletion.
  1. +1 −1 numpy/ma/core.py
  2. +32 −0 numpy/ma/tests/test_core.py
View
2 numpy/ma/core.py
@@ -2897,7 +2897,7 @@ def view(self, dtype=None, type=None, fill_value=None):
else:
output._fill_value = None
else:
- output._fill_value = fill_value
+ output.fill_value = fill_value
return output
view.__doc__ = ndarray.view.__doc__
View
32 numpy/ma/tests/test_core.py
@@ -1554,6 +1554,38 @@ def test_fillvalue_as_arguments(self):
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):

0 comments on commit 4bacc46

Please sign in to comment.