8000 Auto merge of #5967 - wimglenn:bugfix/empty_string_array, r=seberg · numpy/numpy@8c86a0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c86a0a

Browse files
committed
Auto merge of #5967 - wimglenn:bugfix/empty_string_array, r=seberg
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 ```
2 parents 7e04882 + 8749a9a commit 8c86a0a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,9 +2297,13 @@ STRING_nonzero (char *ip, PyArrayObject *ap)
22972297
int len = PyArray_DESCR(ap)->elsize;
22982298
int i;
22992299
npy_bool nonz = NPY_FALSE;
2300+
npy_bool seen_null = NPY_FALSE;
23002301

23012302
for (i = 0; i < len; i++) {
2302-
if (!Py_STRING_ISSPACE(*ip)) {
2303+
if (*ip == '\0') {
2304+
seen_null = NPY_TRUE;
2305+
}
2306+
else if (seen_null || !Py_STRING_ISSPACE(*ip)) {
23032307
nonz = NPY_TRUE;
23042308
break;
23052309
}
@@ -2320,6 +2324,7 @@ UNICODE_nonzero (npy_ucs4 *ip, PyArrayObject *ap)
23202324
int len = PyArray_DESCR(ap)->elsize >> 2;
23212325
int i;
23222326
npy_bool nonz = NPY_FALSE;
2327+
npy_bool seen_null = NPY_FALSE;
23232328
char *buffer = NULL;
23242329

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

23372342
for (i = 0; i < len; i++) {
2338-
if (!PyArray_UCS4_ISSPACE(*ip)) {
2343+
if (*ip == '\0') {
2344+
seen_null = NPY_TRUE;
2345+
}
2346+
else if (seen_null || !PyArray_UCS4_ISSPACE(*ip)) {
23392347
nonz = NPY_TRUE;
23402348
break;
23412349
}

numpy/core/tests/test_multiarray.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5731,5 +5731,47 @@ def test_subclass_other(self):
57315731
assert_(isinstance(f(b, a), self.Other), msg)
57325732

57335733

5734+
class TestBytestringArrayNonzero(TestCase):
5735+
5736+
def test_empty_bstring_array_is_falsey(self):
5737+
self.assertFalse(np.array([''], dtype=np.str))
5738+
5739+
def test_whitespace_bstring_array_is_falsey(self):
5740+
a = np.array(['spam'], dtype=np.str)
5741+
a[0] = ' \0\0'
5742+
self.assertFalse(a)
5743+
5744+
def test_all_null_bstring_array_is_falsey(self):
5745+
a = np.array(['spam'], dtype=np.str)
5746+
a[0] = '\0\0\0\0'
5747+
self.assertFalse(a)
5748+
5749+
def test_null_inside_bstring_array_is_truthy(self):
5750+
a = np.array(['spam'], dtype=np.str)
5751+
a[0] = ' \0 \0'
5752+
self.assertTrue(a)
5753+
5754+
5755+
class TestUnicodeArrayNonzero(TestCase):
5756+
5757+
def test_empty_ustring_array_is_falsey(self):
5758+
self.assertFalse(np.array([''], dtype=np.unicode))
5759+
5760+
def test_whitespace_ustring_array_is_falsey(self):
5761+
a = np.array(['eggs'], dtype=np.unicode)
5762+
a[0] = ' \0\0'
5763+
self.assertFalse(a)
5764+
5765+
def test_all_null_ustring_array_is_falsey(self):
5766+
a = np.array(['eggs'], dtype=np.unicode)
5767+
a[0] = '\0\0\0\0'
5768+
self.assertFalse(a)
5769+
5770+
def test_null_inside_ustring_array_is_truthy(self):
5771+
a = np.array(['eggs'], dtype=np.unicode)
5772+
a[0] = ' \0 \0'
5773+
self.assertTrue(a)
5774+
5775+
57345776
if __name__ == "__main__":
57355777
run_module_suite()

0 commit comments

Comments
 (0)
0