0

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?

Sounak
  • 4,803
  • 7
  • 30
  • 48

2 Answers2

2

f['numbers'][:]+=5 works.

f['numbers'] + 5 does not work because the Dataset object does not have methods like __add__. So the Python interpreter gives you the unsupported error.

Adding the [:] gives an ndarray, with the full set of numpy methods.

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 load 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.

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
1

You have to use the actual numpy.add function:

ds = f['numbers']
ds[:] = np.add(ds, 5)

(Though I do like your syntax better. Maybe that's worth suggesting to the h5py people.)

Carsten
  • 17,991
  • 4
  • 48
  • 53