H5py: fancy indexing via a mix of NumPy array and list not working for v3.1.0

Created on 8 Nov 2020  路  2Comments  路  Source: h5py/h5py

The fancy indexing using a mix of NumPy boolean array and an explicit list does not seem to work in the latest h5py version 3.1.0. The detailed trackback is shown at the bottom.

The exact same commands work in h5py version 2.10.

In h5py version 3.1.0, fancy indexing using NumPy boolean array only or using explicit lists only also works.

To assist reproducing bugs, please include the following:

  • Operating System (MacOS 10.14)
  • Python version (3.7 & 3.9)
  • Where Python was acquired (Anaconda on MacOS)
  • h5py version (3.1)
  • HDF5 version (1.10.6)
  • The full traceback/stack trace shown (if it appears)

```python
(insar) yunjunz:~>$ python
Python 3.7.8 | packaged by conda-forge | (default, Jul 31 2020, 02:37:09)
[Clang 10.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import numpy as np
import h5py
arr = np.arange(100).reshape((10,10))
with h5py.File('test.h5', 'w') as f:
... dset = f.create_dataset("MyDataset", data=arr)
... result = dset[arr[:,0] > 20, :]
... result.shape
...
Traceback (most recent call last):
File "", line 3, in
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "/Users/yunjunz/tools/miniconda3/envs/insar/lib/python3.7/site-packages/h5py/_hl/dataset.py", line 777, in __getitem__
selection = sel.select(self.shape, args, dataset=self)
File "/Users/yunjunz/tools/miniconda3/envs/insar/lib/python3.7/site-packages/h5py/_hl/selections.py", line 82, in select
return selector.make_selection(args)
File "h5py/_selector.pyx", line 272, in h5py._selector.Selector.make_selection
File "h5py/_selector.pyx", line 183, in h5py._selector.Selector.apply_args
TypeError: Indexing arrays must have integer dtypes```

Most helpful comment

Sorry about that. As a workaround, you can convert the 1D boolean array to an array of indices with .nonzero():

result = dset[(arr[:, 0] > 20).nonzero()[0]]

All 2 comments

Sorry about that. As a workaround, you can convert the 1D boolean array to an array of indices with .nonzero():

result = dset[(arr[:, 0] > 20).nonzero()[0]]

Thanks for the workaround solution @takluyver.

Was this page helpful?
0 / 5 - 0 ratings