summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2017-10-14 08:05:06 -0600
committerGitHub <noreply@github.com>2017-10-14 08:05:06 -0600
commitd4afa3eba6c9af2a7b85586adbe2801505eaf3ff (patch)
tree901386c48aa67f831b77b52f47b622dcb81cbb41
parent8f2da8bbb2c398d3c4ab4406218dfe659956ea86 (diff)
parent931758e2e25e9a2628c0dc953744fa4db9ca657a (diff)
downloadnumpy-d4afa3eba6c9af2a7b85586adbe2801505eaf3ff.tar.gz
Merge pull request #9856 from eric-wieser/fix-void-bool-2
BUG: Make bool(void_scalar) and void_scalar.astype(bool) consistent
-rw-r--r--doc/release/1.14.0-notes.rst8
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src7
-rw-r--r--numpy/core/tests/test_multiarray.py31
3 files changed, 42 insertions, 4 deletions
diff --git a/doc/release/1.14.0-notes.rst b/doc/release/1.14.0-notes.rst
index 20097e97e..2c99e13e8 100644
--- a/doc/release/1.14.0-notes.rst
+++ b/doc/release/1.14.0-notes.rst
@@ -128,6 +128,14 @@ This is for pytest compatibility in the case of duplicate test file names in
the different directories. As a result, ``run_module_suite`` no longer works,
i.e., ``python <path-to-test-file>`` results in an error.
+``.astype(bool)`` on unstructured void arrays now calls ``bool`` on each element
+--------------------------------------------------------------------------------
+On Python 2, ``void_array.astype(bool)`` would always return an array of
+``True``, unless the dtype is ``V0``. On Python 3, this operation would usually
+crash. Going forwards, `astype` matches the behavior of ``bool(np.void)``,
+considering a buffer of all zeros as false, and anything else as true.
+Checks for ``V0`` can still be done with ``arr.dtype.itemsize == 0``.
+
``MaskedArray.squeeze`` never returns ``np.ma.masked``
------------------------------------------------------
``np.squeeze`` is documented as returning a view, but the masked variant would
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index 0826df1c3..6023365ed 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -1561,12 +1561,12 @@ static void
PyArrayObject *aip = vaip;
npy_intp i;
- PyObject *temp = NULL, *new;
int skip = PyArray_DESCR(aip)->elsize;
int oskip = @oskip@;
for (i = 0; i < n; i++, ip+=skip, op+=oskip) {
- temp = @from@_getitem(ip, aip);
+ PyObject *new;
+ PyObject *temp = PyArray_Scalar(ip, PyArray_DESCR(aip), (PyObject *)aip);
if (temp == NULL) {
return;
}
@@ -1621,12 +1621,11 @@ static void
PyArrayObject *aip = vaip;
npy_intp i;
- PyObject *temp = NULL;
int skip = PyArray_DESCR(aip)->elsize;
int oskip = @oskip@;
for (i = 0; i < n; i++, ip+=skip, op+=oskip) {
- temp = @from@_getitem(ip, aip);
+ PyObject *temp = PyArray_Scalar(ip, PyArray_DESCR(aip), (PyObject *)aip);
if (temp == NULL) {
return;
}
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index e327717b1..92fc21b83 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1203,6 +1203,37 @@ class TestBool(object):
a[:o] = False
assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
+ def _test_cast_from_flexible(self, dtype):
+ # empty string -> false
+ for n in range(3):
+ v = np.array(b'', (dtype, n))
+ assert_equal(bool(v), False)
+ assert_equal(bool(v[()]), False)
+ assert_equal(v.astype(bool), False)
+ assert_(isinstance(v.astype(bool), np.ndarray))
+ assert_(v[()].astype(bool) is np.False_)
+
+ # anything else -> true
+ for n in range(1, 4):
+ for val in [b'a', b'0', b' ']:
+ v = np.array(val, (dtype, n))
+ assert_equal(bool(v), True)
+ assert_equal(bool(v[()]), True)
+ assert_equal(v.astype(bool), True)
+ assert_(isinstance(v.astype(bool), np.ndarray))
+ assert_(v[()].astype(bool) is np.True_)
+
+ def test_cast_from_void(self):
+ self._test_cast_from_flexible(np.void)
+
+ @dec.knownfailureif(True, "See gh-9847")
+ def test_cast_from_unicode(self):
+ self._test_cast_from_flexible(np.unicode_)
+
+ @dec.knownfailureif(True, "See gh-9847")
+ def test_cast_from_bytes(self):
+ self._test_cast_from_flexible(np.bytes_)
+
class TestZeroSizeFlexible(object):
@staticmethod