diff --git a/Changelog b/Changelog index a68911d95..41baa59c7 100644 --- a/Changelog +++ b/Changelog @@ -2,6 +2,9 @@ ========================== * Added ``netCDF4.__has_set_alignment__`` property to help identify if the underlying netcdf4 supports setting the HDF5 alignment. + * Slicing multi-dimensional variables with an all False boolean index array + now returns an empty numpy array (instead of raising an exception - issue #1197). + Behavior now consistent with numpy slicing. version 1.6.1 (tag v1.6.1rel) ============================== diff --git a/src/netCDF4/_netCDF4.pyx b/src/netCDF4/_netCDF4.pyx index 5acd32c8d..29852a65d 100644 --- a/src/netCDF4/_netCDF4.pyx +++ b/src/netCDF4/_netCDF4.pyx @@ -4953,7 +4953,7 @@ rename a `Variable` attribute named `oldname` to `newname`.""" # put_ind for this dimension is set to -1 by _StartCountStride. squeeze = data.ndim * [slice(None),] for i,n in enumerate(put_ind.shape[:-1]): - if n == 1 and put_ind[...,i].ravel()[0] == -1: + if n == 1 and put_ind.size > 0 and put_ind[...,i].ravel()[0] == -1: squeeze[i] = 0 # Reshape the arrays so we can iterate over them. diff --git a/src/netCDF4/utils.py b/src/netCDF4/utils.py index c96cc7575..d0fd389b1 100644 --- a/src/netCDF4/utils.py +++ b/src/netCDF4/utils.py @@ -238,6 +238,10 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\ unlim = False # convert boolean index to integer array. if np.iterable(ea) and ea.dtype.kind =='b': + # check that boolean array is not all False. + if not ea.any(): + msg='Boolean index array is all False. Empty array will be returned.' + warnings.warn(msg) # check that boolean array not too long if not unlim and shape[i] != len(ea): msg=""" @@ -457,7 +461,7 @@ def _out_array_shape(count): out = [] for i, n in enumerate(s): - if n == 1: + if n == 1 and count.size > 0: c = count[..., i].ravel()[0] # All elements should be identical. out.append(c) else: diff --git a/test/tst_fancyslicing.py b/test/tst_fancyslicing.py index dd359a52d..38611b3cb 100644 --- a/test/tst_fancyslicing.py +++ b/test/tst_fancyslicing.py @@ -142,6 +142,11 @@ def test_get(self): assert_array_equal(v[0], self.data[0]) + # slicing with all False booleans (PR #1197) + iby[:] = False + data = v[ibx,iby,ibz] + assert(data.size == 0) + f.close() def test_set(self):