+-
python – 我应该如何使用h5py lib存储时间序列数据
我有一些时间序列数据,我以前使用pytables存储为hdf5文件.我最近尝试用h5py lib存储它.但是,由于numpy数组的所有元素必须是相同的dtype,我必须在使用h5py lib存储它之前将日期(通常是索引)转换为’float64’类型.当我使用pytables时,索引及其dtype被保留,这使我可以查询时间序列而无需将其全部拉入内存.我想用h5py是不可能的.我在这里错过了什么吗?如果没有,在什么情况下我应该使用h5py lib来存储时间序列数据?我问这个问题的原因,清楚这可以帮助我设计一个更有效(处理和存储明智)的项目.

下面是简单的代码,我必须丢失索引信息,以便将其存储为单个dtype对象

dt_range = pd.date_range('2016-12-01','2016-12-10')
data = np.arange(0,20).reshape(-1,2)
df = pd.DataFrame(data,index = dt_range, columns = list('ab'), dtype = 'float')
df.index  = df.index.to_julian_date()
df = df.reset_index()
h = h5py.File(r'path\temp.h5', 'w')
dset = h.create_dataset('temp',data = df.values, shape = (10,3))
最佳答案
我会用pandas to_hdf

dt_range = pd.date_range('2016-12-01','2016-12-10')
data = np.arange(0,20).reshape(-1,2)
df = pd.DataFrame(data,index = dt_range, columns = list('ab'), dtype = 'float')
df.index  = df.index.to_julian_date()
df = df.reset_index()

with pd.HDFStore('temp.h5', 'w') as h:
    df.to_hdf(h, 'temp')

pd.read_hdf('temp.h5', 'temp')

enter image description here

点击查看更多相关文章

转载注明原文:python – 我应该如何使用h5py lib存储时间序列数据 - 乐贴网