Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more descriptive error message if indexing with all False bool … #1197

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog
Expand Up @@ -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)
==============================
Expand Down
2 changes: 1 addition & 1 deletion src/netCDF4/_netCDF4.pyx
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/netCDF4/utils.py
Expand Up @@ -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="""
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions test/tst_fancyslicing.py
Expand Up @@ -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):
Expand Down