8000 Add boolean scalar error message · melissawm/numpy@eb3db92 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb3db92

Browse files
committed
Add boolean scalar error message
1 parent e723d39 commit eb3db92

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

numpy/_core/src/multiarray/item_selection.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,9 +2798,19 @@ PyArray_Nonzero(PyArrayObject *self)
27982798
{
27992799
int i, ndim = PyArray_NDIM(self);
28002800
if (ndim == 0) {
2801-
PyErr_SetString(PyExc_ValueError,
2802-
"Calling nonzero on 0d arrays is not allowed. "
2803-
"Use np.atleast_1d(scalar).nonzero() instead.");
2801+
char const* msg;
2802+
if (PyArray_ISBOOL(self)) {
2803+
msg =
2804+
"Calling nonzero on 0d arrays is not allowed. "
2805+
"Use np.atleast_1d(scalar).nonzero() instead. "
2806+
"If the context of this error is of the form "
2807+
"`arr[nonzero(cond)]`, just use `arr[cond]`.";
2808+
} else {
2809+
msg =
2810+
"Calling nonzero on 0d arrays is not allowed. "
2811+
"Use np.atleast_1d(scalar).nonzero() instead.";
2812+
}
2813+
PyErr_SetString(PyExc_ValueError, msg);
28042814
return NULL;
28052815
}
28062816

0 commit comments

Comments
 (0)
0