I want to add a constant value to an array. The array is stored in an hdf5 file.
f = h5py.File(fileName)
f['numbers'] = f['numbers'] + 5
gives me an error that
TypeError: unsupported operand type(s) for +: 'Dataset' and 'int'
how should I do it?
f['numbers'] + 5 does not work because the Dataset object does not have methods like
__add__. So the Python interpreter gives you the
[:] gives an
ndarray, with the full set of
Doesn't the documentation talk about loading slices of the data into memory?
`f['numbers'][:10] += 5
might work as way to change a portion. The addition is still being done in memory.
See earlier questions like How to store an array in hdf5 file which is too big to store in memory
Another option is to look at the compiled
h5 code. There probably are Fortran or C based scripts which will make changes to the data like this. You could easily call those from Python.
You have to use the actual
ds = f['numbers'] ds[:] = np.add(ds, 5)
(Though I do like your syntax better. Maybe that's worth suggesting to the h5py people.)