使用h5py强制hdf5文件的数据类型

时间:2015-12-22 18:05:38

标签: python numpy hdf5 h5py

我有一个csv文件,其中包含“日期”,“时间”和其他列(10个左右)

Date,Time,C
20020515,123000000,10293
20020515,160000000,10287
20020516,111800000,10270
20020516,160000000,10260
20020517,130500000,10349
20020517,160000000,10276
20020520,123700000,10313
20020520,160000000,10258
20020521,114500000,10223

我正在尝试将其加载到hdf5文件中,并且日期和时间类型为“String”而不是integer32。所以我这样做

import h5py,numpy as np
my_data = np.genfromtxt("/tmp/data.txt",delimiter=",",dtype=None,names=True)
myFile="/tmp/data.h5"
with h5py.File(myFile,"a") as f:
  dset = f.create_dataset('foo',data=my_data)

我想在HDF5上将“Date”和“Time”存储为“String”类型。不是Int32。

1 个答案:

答案 0 :(得分:4)

一个简单的解决方案是在将my_data写入文件之前更改newtype = np.dtype([('Date', 'S8'), ('Time', 'S8'), ('C', '<i8')]) dset2 = f.create_dataset('foo2', data=my_data.astype(newtype)) 的dtype:

dtype=

您还可以通过将相应的shape=f.create_dataset参数传递给my_data来创建空数据集,然后填写dset3 = f.create_dataset('foo3', shape=my_data.shape, dtype=newtype) dset3[:] = my_data.astype(newtype) 中的值:

my_data

请注意,在编写之前我仍然必须将newtype强制转换为In [15]: dset3[:] = my_data --------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-15-6e62dae3d59a> in <module>() ----> 1 dset3[:] = my_data h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/tmp/pip-build-aayglkf0/h5py/h5py/_objects.c:2579)() h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/tmp/pip-build-aayglkf0/h5py/h5py/_objects.c:2538)() /home/alistair/.venvs/core3/lib/python3.4/site-packages/h5py/_hl/dataset.py in __setitem__(self, args, val) 584 mspace = h5s.create_simple(mshape_pad, (h5s.UNLIMITED,)*len(mshape_pad)) 585 for fspace in selection.broadcast(mshape): --> 586 self.id.write(mspace, fspace, val, mtype) 587 588 def read_direct(self, dest, source_sel=None, dest_sel=None): h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/tmp/pip-build-aayglkf0/h5py/h5py/_objects.c:2579)() h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/tmp/pip-build-aayglkf0/h5py/h5py/_objects.c:2538)() h5py/h5d.pyx in h5py.h5d.DatasetID.write (/tmp/pip-build-aayglkf0/h5py/h5py/h5d.c:3421)() h5py/_proxy.pyx in h5py._proxy.dset_rw (/tmp/pip-build-aayglkf0/h5py/h5py/_proxy.c:1794)() h5py/_proxy.pyx in h5py._proxy.H5PY_H5Dwrite (/tmp/pip-build-aayglkf0/h5py/h5py/_proxy.c:1501)() OSError: Can't prepare for writing data (No appropriate function for conversion path) - h5py似乎无法自行处理类型转换:

{{1}}