summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2010-02-08 06:21:33 +0000
committerpierregm <pierregm@localhost>2010-02-08 06:21:33 +0000
commit53f1ee985c07924eec61133f8c6b8f27c481ac84 (patch)
treed75b78bd6804fe93648825465564e61339c61f30
parent5bad51bf3afc9d2256e9f6a5a1883233b3065ddd (diff)
downloadnumpy-53f1ee985c07924eec61133f8c6b8f27c481ac84.tar.gz
* Make sure _fill_value is never None for structured masked arrays
-rw-r--r--numpy/ma/core.py29
-rw-r--r--numpy/ma/tests/test_core.py15
2 files changed, 38 insertions, 6 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 3ab29ae6b..f4aa16d0c 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -2792,6 +2792,10 @@ class MaskedArray(ndarray):
except AttributeError:
# When _mask.shape is not writable (because it's a void)
pass
+ # Finalize the fill_value for structured arrays
+ if self.dtype.names:
+ if self._fill_value is None:
+ self._fill_value = _check_fill_value(None, self.dtype)
return
@@ -3569,7 +3573,8 @@ class MaskedArray(ndarray):
elif n <= 1:
return _print_templates['short'] % parameters
return _print_templates['long'] % parameters
- #............................................
+
+
def __eq__(self, other):
"Check whether other equals self elementwise"
if self is masked:
@@ -5432,7 +5437,9 @@ class mvoid(MaskedArray):
#
def __new__(self, data, mask=nomask, dtype=None, fill_value=None):
dtype = dtype or data.dtype
- _data = ndarray.__new__(self, (), dtype=dtype, buffer=data.data)
+ _data = ndarray((), dtype=dtype)
+ _data[()] = data
+ _data = _data.view(self)
if mask is not nomask:
try:
# Mask is already a 0D array
@@ -5463,17 +5470,29 @@ class mvoid(MaskedArray):
return self._data.__str__()
m = tuple(m)
if (not any(m)):
- return self._data.__repr__()
+ return self._data.__str__()
r = self._data.tolist()
p = masked_print_option
if not p.enabled():
p = 'N/A'
else:
p = str(p)
- r = [(str(_), p)[_m] for (_, _m) in zip(self._data.tolist(), tuple(m))]
+ r = [(str(_), p)[_m] for (_, _m) in zip(r, m)]
return "(%s)" % ", ".join(r)
- __repr__ = __str__
+ def __repr__(self):
+ m = self._mask
+ if (m is nomask):
+ return self._data.__repr__()
+ m = tuple(m)
+ if not any(m):
+ return self._data.__repr__()
+ p = masked_print_option
+ if not p.enabled():
+ return self.filled(self.fill_value).__repr__()
+ p = str(p)
+ r = [(str(_), p)[_m] for (_, _m) in zip(r, m)]
+ return "(%s)" % ", ".join(r)
def __iter__(self):
"Defines an iterator for mvoid"
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index b849d0c48..74bd2f722 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -503,7 +503,7 @@ class TestMaskedArray(TestCase):
def test_filled_w_mvoid(self):
"Test filled w/ mvoid"
ndtype = [('a', int), ('b', float)]
- a = mvoid(np.array((1, 2)), mask=[(0, 1)], dtype=ndtype)
+ a = mvoid((1, 2.), mask=[(0, 1)], dtype=ndtype)
# Filled using default
test = a.filled()
assert_equal(tuple(test), (1, default_fill_value(1.)))
@@ -1457,6 +1457,19 @@ class TestFillingValues(TestCase):
assert_equal(tt._fill_value, np.array(10))
assert_equal(tuple(t.fill_value), (10, default_fill_value(0)))
+ def test_fillvalue_implicit_structured_array(self):
+ "Check that fill_value is always defined for structured arrays"
+ ndtype = ('b', float)
+ adtype = ('a', float)
+ a = array([(1.,), (2.,)], mask=[(False,), (False,)],
+ fill_value=(np.nan,), dtype=np.dtype([adtype]))
+ b = empty(a.shape, dtype=[adtype, ndtype])
+ b['a'] = a['a']
+ b['a'].set_fill_value(a['a'].fill_value)
+ f = b._fill_value[()]
+ assert(np.isnan(f[0]))
+ assert_equal(f[-1], default_fill_value(1.))
+
#------------------------------------------------------------------------------
class TestUfuncs(TestCase):