Skip to content

Commit

Permalink
Auto merge of #5967 - wimglenn:bugfix/empty_string_array, r=seberg
Browse files Browse the repository at this point in the history
BUG: fix inconsistency with np.array(['']) being truthy

```
a = np.array([0])
b = np.array([None])
c = np.array([''])
d = np.array(['   '])
```

Why should we have this inconsistency:

```
>>> bool(a)
False
>>> bool(b)
False
>>> bool(c)
True
>>> bool(d)
False
```
  • Loading branch information
homu committed Jun 16, 2015
2 parents 7e04882 + 8749a9a commit 8c86a0a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
12 changes: 10 additions & 2 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2297,9 +2297,13 @@ STRING_nonzero (char *ip, PyArrayObject *ap)
int len = PyArray_DESCR(ap)->elsize;
int i;
npy_bool nonz = NPY_FALSE;
npy_bool seen_null = NPY_FALSE;

for (i = 0; i < len; i++) {
if (!Py_STRING_ISSPACE(*ip)) {
if (*ip == '\0') {
seen_null = NPY_TRUE;
}
else if (seen_null || !Py_STRING_ISSPACE(*ip)) {
nonz = NPY_TRUE;
break;
}
Expand All @@ -2320,6 +2324,7 @@ UNICODE_nonzero (npy_ucs4 *ip, PyArrayObject *ap)
int len = PyArray_DESCR(ap)->elsize >> 2;
int i;
npy_bool nonz = NPY_FALSE;
npy_bool seen_null = NPY_FALSE;
char *buffer = NULL;

if ((!PyArray_ISNOTSWAPPED(ap)) || (!PyArray_ISALIGNED(ap))) {
Expand All @@ -2335,7 +2340,10 @@ UNICODE_nonzero (npy_ucs4 *ip, PyArrayObject *ap)
}

for (i = 0; i < len; i++) {
if (!PyArray_UCS4_ISSPACE(*ip)) {
if (*ip == '\0') {
seen_null = NPY_TRUE;
}
else if (seen_null || !PyArray_UCS4_ISSPACE(*ip)) {
nonz = NPY_TRUE;
break;
}
Expand Down
42 changes: 42 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5731,5 +5731,47 @@ def test_subclass_other(self):
assert_(isinstance(f(b, a), self.Other), msg)


class TestBytestringArrayNonzero(TestCase):

def test_empty_bstring_array_is_falsey(self):
self.assertFalse(np.array([''], dtype=np.str))

def test_whitespace_bstring_array_is_falsey(self):
a = np.array(['spam'], dtype=np.str)
a[0] = ' \0\0'
self.assertFalse(a)

def test_all_null_bstring_array_is_falsey(self):
a = np.array(['spam'], dtype=np.str)
a[0] = '\0\0\0\0'
self.assertFalse(a)

def test_null_inside_bstring_array_is_truthy(self):
a = np.array(['spam'], dtype=np.str)
a[0] = ' \0 \0'
self.assertTrue(a)


class TestUnicodeArrayNonzero(TestCase):

def test_empty_ustring_array_is_falsey(self):
self.assertFalse(np.array([''], dtype=np.unicode))

def test_whitespace_ustring_array_is_falsey(self):
a = np.array(['eggs'], dtype=np.unicode)
a[0] = ' \0\0'
self.assertFalse(a)

def test_all_null_ustring_array_is_falsey(self):
a = np.array(['eggs'], dtype=np.unicode)
a[0] = '\0\0\0\0'
self.assertFalse(a)

def test_null_inside_ustring_array_is_truthy(self):
a = np.array(['eggs'], dtype=np.unicode)
a[0] = ' \0 \0'
self.assertTrue(a)


if __name__ == "__main__":
run_module_suite()

0 comments on commit 8c86a0a

Please sign in to comment.