diff options
author | Travis Oliphant <oliphant@enthought.com> | 2007-03-30 18:19:07 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2007-03-30 18:19:07 +0000 |
commit | 9c6f421934530cda2e33487bae3295519335597c (patch) | |
tree | 523d8ee6e2078c756c105e10c7293692c2fa6602 /numpy/core | |
parent | ef392181a8ed3e9f2646745b19aedbc3d1145c95 (diff) | |
download | numpy-9c6f421934530cda2e33487bae3295519335597c.tar.gz |
Allow Boolean mask indexing for 0-d arrays.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/ma.py | 8 | ||||
-rw-r--r-- | numpy/core/src/arrayobject.c | 40 | ||||
-rw-r--r-- | numpy/core/tests/test_ma.py | 2 |
3 files changed, 41 insertions, 9 deletions
diff --git a/numpy/core/ma.py b/numpy/core/ma.py index d980fbf44..224cc4753 100644 --- a/numpy/core/ma.py +++ b/numpy/core/ma.py @@ -1264,14 +1264,6 @@ array(data = %(data)s, value = numeric.array(value, dtype=object) d = d.astype(object) result = fromnumeric.choose(m, (d, value)) - except IndexError: - #ok, if scalar - if d.shape: - raise - elif m: - result = numeric.array(value, dtype=d.dtype) - else: - result = d return result def ids (self): diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 582421ee2..9de78f996 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -2720,6 +2720,25 @@ array_subscript(PyArrayObject *self, PyObject *op) return NULL; return add_new_axes_0d(self, nd); } + /* Allow Boolean mask selection also */ + if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || + (PyArray_Check(op) && (PyArray_DIMS(op)==0) && + PyArray_ISBOOL(op))) { + if (PyObject_IsTrue(op)) { + Py_INCREF(self); + return (PyObject *)self; + } + else { + oned = 0; + Py_INCREF(self->descr); + return PyArray_NewFromDescr(self->ob_type, + self->descr, + 1, &oned, + NULL, NULL, + NPY_DEFAULT, + NULL); + } + } PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed."); return NULL; @@ -2886,10 +2905,27 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op) } if (self->nd == 0) { + /* Several different exceptions to the 0-d no-indexing rule + + 1) ellipses + 2) empty tuple + 3) Using newaxis (None) + 4) Boolean mask indexing + */ if (index == Py_Ellipsis || index == Py_None || \ (PyTuple_Check(index) && (0 == PyTuple_GET_SIZE(index) || \ count_new_axes_0d(index) > 0))) return self->descr->f->setitem(op, self->data, self); + if (PyBool_Check(index) || PyArray_IsScalar(index, Bool) || + (PyArray_Check(index) && (PyArray_DIMS(index)==0) && + PyArray_ISBOOL(index))) { + if (PyObject_IsTrue(index)) { + return self->descr->f->setitem(op, self->data, self); + } + else { /* don't do anything */ + return 0; + } + } PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed."); return -1; @@ -3008,6 +3044,10 @@ array_subscript_nice(PyArrayObject *self, PyObject *op) Bool noellipses = TRUE; if (op == Py_Ellipsis) noellipses = FALSE; + else if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || + (PyArray_Check(op) && (PyArray_DIMS(op)==0) && + PyArray_ISBOOL(op))) + noellipses = FALSE; else if (PySequence_Check(op)) { int n, i; PyObject *temp; diff --git a/numpy/core/tests/test_ma.py b/numpy/core/tests/test_ma.py index 00bd5c86d..a74638a23 100644 --- a/numpy/core/tests/test_ma.py +++ b/numpy/core/tests/test_ma.py @@ -611,7 +611,7 @@ class test_ma(NumpyTestCase): self.failUnless(minimum(xm, xm).mask) self.failUnless(xm.filled().dtype is xm.data.dtype) x = array(0, mask=0) - self.failUnless(x.filled() is x.data) + self.failUnless(x.filled() == x.data) self.failUnlessEqual(str(xm), str(masked_print_option)) def check_testArrayMethods(self): |