How to resize an HDF5 array with `h5py`

MRocklin picture MRocklin · Apr 10, 2014 · Viewed 9.8k times · Source

How can I resize an HDF5 array using the h5py Python library ?

I've tried using the .resize method and on an array with chunks set to True. Alas, I'm still missing something.

In [1]: import h5py

In [2]: f = h5py.File('foo.hdf5', 'w')

In [3]: d = f.create_dataset('data', (3, 3), dtype='i8', chunks=True)

In [4]: d.resize((6, 3))
/home/mrocklin/Software/anaconda/lib/python2.7/site-packages/h5py/_hl/dataset.pyc in resize(self, size, axis)
--> 277         self.id.set_extent(size)
ValueError: unable to set extend dataset (Dataset: Unable to initialize object)

In [11]: h5py.__version__ 
Out[11]: '2.2.1'

Answer

user130604 picture user130604 · Oct 6, 2014

As mentioned by Oren, you need to use maxshape when creating the dataset if you want to change the array size later. Setting a dimension to None allows you to resize that dimension up to 2**64 (h5's limit) later:

In [1]: import h5py

In [2]: f = h5py.File('foo.hdf5', 'w')

In [3]: d = f.create_dataset('data', (3, 3), maxshape=(None, 3), dtype='i8', chunks=True)

In [4]: d.resize((6, 3))

In [5]: h5py.__version__
Out[5]: '2.2.1'

See the docs for more.