How can I store NumPy datetime objects in HDF5 using h5py
?
In [1]: import h5py
In [2]: import numpy as np
In [3]: f = h5py.File('foo.hdfs', 'w')
In [4]: d = f.create_dataset('data', shape=(2, 2), dtype=np.datetime64)
TypeError: No conversion path for dtype: dtype('<M8')
Currently h5py does not support a time type (FAQ, Issue).
NumPy datetime64s are 8 bytes long. So as a workaround, you could view the data as '<i8'
store the ints in the hdf5 file, and view it as a np.datetime64
upon retrieval:
import numpy as np
import h5py
arr = np.linspace(0, 10000, 4).astype('<i8').view('<M8[D]').reshape((2,2))
print(arr)
# [['1970-01-01' '1979-02-16']
# ['1988-04-02' '1997-05-19']]
with h5py.File('/tmp/out.h5', "w") as f:
dset = f.create_dataset('data', (2, 2), '<i8')
dset[:,:] = arr.view('<i8')
with h5py.File('/tmp/out.h5', "r") as f:
dset = f.get('data')
print(dset.value.view('<M8[D]'))
# [['1970-01-01' '1979-02-16']
# ['1988-04-02' '1997-05-19']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With