deal with the case when input is boolean ndarray of size 0
@@ -2399,6 +2399,14 @@ cpdef _prepare_mask_indexing_single(ndarray a, ndarray mask, int axis):
cdef int n_true
cdef tuple lshape, rshape, out_shape
+ lshape = a.shape[:axis]
+ rshape = a.shape[axis + mask.ndim:]
+
+ if mask.size == 0:
+ masked_shape = lshape + (0,) + rshape
+ mask_br = mask._reshape(masked_shape)
+ return mask_br, mask_br, masked_shape
+
# Get number of True in the mask to determine the shape of the array
# after masking.
if mask.size <= 2 ** 31 - 1:
@@ -2407,8 +2415,6 @@ cpdef _prepare_mask_indexing_single(ndarray a, ndarray mask, int axis):
mask_type = numpy.int64
mask_scanned = scan(mask.astype(mask_type).ravel()) # starts with 1
n_true = int(mask_scanned[-1])
- lshape = a.shape[:axis]
- rshape = a.shape[axis + mask.ndim:]
masked_shape = lshape + (n_true,) + rshape
# When mask covers the entire array, broadcasting is not necessary.
@@ -2435,6 +2441,8 @@ cpdef ndarray _getitem_mask_single(ndarray a, ndarray mask, int axis):
mask, mask_scanned, masked_shape = _prepare_mask_indexing_single(
a, mask, axis)
out = ndarray(masked_shape, dtype=a.dtype)
+ if out.size == 0:
+ return out
return _getitem_mask_kernel(a, mask, mask_scanned, out)
