summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2010-02-21 02:52:56 +0000
committerPauli Virtanen <pav@iki.fi>2010-02-21 02:52:56 +0000
commit79430a42f67c579bc69aa375237201d2dde7c636 (patch)
treef1c4b0661f27e8bc6f1112aac1b27ef71e31e150
parent3f8f41bb3150b99407b3bbc9bac087fe07ceb009 (diff)
downloadnumpy-79430a42f67c579bc69aa375237201d2dde7c636.tar.gz
BUG: ma: _check_fill_value shouldn't rely on implicit array() string casting
Since array([12345678.9, 'a']) == array(['12345678', 'a'], dtype='|S8') ie., automatic string conversion uses only the size of the minimal data type, not the size of the string representation, code should not rely on array() casting items implicitly to object arrays.
-rw-r--r--numpy/ma/core.py1
-rw-r--r--numpy/ma/tests/test_core.py26
2 files changed, 14 insertions, 13 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index cda940962..f6d2d1e6a 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -405,6 +405,7 @@ def _check_fill_value(fill_value, ndtype):
raise ValueError(err_msg % (fill_value, fdtype))
else:
descr = ndtype.descr
+ fill_value = np.asarray(fill_value, dtype=object)
fill_value = np.array(_recursive_set_fill_value(fill_value, descr),
dtype=ndtype)
else:
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 40b7b4235..5a02e554b 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -1307,41 +1307,41 @@ class TestFillingValues(TestCase):
_check_fill_value = np.ma.core._check_fill_value
ndtype = [('a', int), ('b', float), ('c', "|S3")]
# A check on a list should return a single record
- fval = _check_fill_value([-999, -999.9, "???"], ndtype)
+ fval = _check_fill_value([-999, -12345678.9, "???"], ndtype)
self.assertTrue(isinstance(fval, ndarray))
- assert_equal(fval.item(), [-999, -999.9, "???"])
+ assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
# A check on None should output the defaults
fval = _check_fill_value(None, ndtype)
self.assertTrue(isinstance(fval, ndarray))
assert_equal(fval.item(), [default_fill_value(0),
default_fill_value(0.),
- default_fill_value("0")])
+ asbytes(default_fill_value("0"))])
#.....Using a structured type as fill_value should work
- fill_val = np.array((-999, -999.9, "???"), dtype=ndtype)
+ fill_val = np.array((-999, -12345678.9, "???"), dtype=ndtype)
fval = _check_fill_value(fill_val, ndtype)
self.assertTrue(isinstance(fval, ndarray))
- assert_equal(fval.item(), [-999, -999.9, "???"])
+ assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
#.....Using a flexible type w/ a different type shouldn't matter
- fill_val = np.array((-999, -999.9, "???"),
+ fill_val = np.array((-999, -12345678.9, "???"),
dtype=[("A", int), ("B", float), ("C", "|S3")])
fval = _check_fill_value(fill_val, ndtype)
self.assertTrue(isinstance(fval, ndarray))
- assert_equal(fval.item(), [-999, -999.9, "???"])
+ assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
#.....Using an object-array shouldn't matter either
- fill_value = np.array((-999, -999.9, "???"), dtype=object)
+ fill_value = np.array((-999, -12345678.9, "???"), dtype=object)
fval = _check_fill_value(fill_val, ndtype)
self.assertTrue(isinstance(fval, ndarray))
- assert_equal(fval.item(), [-999, -999.9, "???"])
+ assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
#
- fill_value = np.array((-999, -999.9, "???"))
+ fill_value = np.array((-999, -12345678.9, "???"))
fval = _check_fill_value(fill_val, ndtype)
self.assertTrue(isinstance(fval, ndarray))
- assert_equal(fval.item(), [-999, -999.9, "???"])
+ assert_equal(fval.item(), [-999, -12345678.9, asbytes("???")])
#.....One-field-only flexible type should work as well
ndtype = [("a", int)]
- fval = _check_fill_value(-999, ndtype)
+ fval = _check_fill_value(-999999999, ndtype)
self.assertTrue(isinstance(fval, ndarray))
- assert_equal(fval.item(), (-999,))
+ assert_equal(fval.item(), (-999999999,))
def test_fillvalue_conversion(self):