Kavan
Kavan

Reputation: 331

Python : Change datatype of hdf5 array

I have hdf5 array as bellow:

>>> a = np.array([5,8])
>>> f = h5py.File('try.hdf5')
>>> f['try'] = a
>>> f['try']
<HDF5 dataset "try": shape (2,), type "<i4">

I want to change datatype of f['try'] to float64. How to do it?

a = a.astype('float64')will do for numpy but I dont know for hdf5.

Upvotes: 2

Views: 2775

Answers (2)

charlie80
charlie80

Reputation: 836

The HDF5 User's Guide (section 6.3.2) clearly says:

The datatype of a dataset can never be changed.

Also see this question.

Upvotes: 1

Kavan
Kavan

Reputation: 331

This one works, but it seems to be time consuming, other solutions are appreciated.

Make a new hdf5 file

>>> f2 = h5py.File('try2.hdf5')
>>> f2['try2'] = f['try'][...].astype('float64')
>>> f2['try2']
<HDF5 dataset "try2": shape (2,), type "<f8">
>>> f['try']
<HDF5 dataset "try": shape (2,), type "<i4">

Upvotes: 0

Related Questions