【问题标题】:How should I use the h5py library for storing time series data?我应该如何使用 h5py 库来存储时间序列数据?
【发布时间】:2017-05-01 14:13:23
【问题描述】:

我有一些时间序列数据,我之前使用pytables 存储为 hdf5 文件。我最近尝试使用h5py lib 存储相同的内容。但是,由于 numpy 数组的所有元素都必须具有相同的 dtype,因此我必须在使用 h5py lib 存储之前将日期(通常是索引)转换为“float64”类型。 当我使用pytables 时,索引及其数据类型被保留,这使我可以查询时间序列,而无需将其全部拉入内存。我想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))

【问题讨论】:

  • 你能用'h5py'读取'tables'创建的文件吗?
  • 我不确定我这样做是否正确,但是虽然 'h5py' 可以识别通过 'pytables' 创建的文件(例如:h.keys() 命令),但当我尝试读取相同的文件时(例如:h['pytable-filename'][:]),它会抛出这个错误:'AttributeError: 'slice' object has no attribute 'encode'
  • 我没有与pytables 合作过。 h5py 使文件数据序列的外观和行为类似于 numpy 数组。这就是您的df.values 正在保存的内容。您可能还需要将 pandas 索引提取到数组中,然后编写它。你知道很多numpy,还是只知道pandas
  • pandas 索引可能可以写为h5 维度比例,但我没有使用过。

标签: python pandas numpy pytables h5py


【解决方案1】:

我会使用熊猫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')

【讨论】:

    【解决方案2】:

    当我运行@piRSquared 代码并查看带有h5py 的文件时,我看到了:

    In [4]: import h5py
    In [5]: f=h5py.File('temp.h5')
    
    In [8]: list(f.keys())
    Out[8]: ['temp']
    In [9]: f['temp']
    Out[9]: <HDF5 group "/temp" (4 members)>
    In [10]: list(f['temp'].keys())
    Out[10]: ['axis0', 'axis1', 'block0_items', 'block0_values']
    
    In [11]: f['temp']['axis0'][:]
    Out[11]: 
    array([b'index', b'a', b'b'], 
          dtype='|S5')
    In [12]: f['temp']['axis1'][:]
    Out[12]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)
    In [13]: f['temp']['block0_items'][:]
    Out[13]: 
    array([b'index', b'a', b'b'], 
          dtype='|S5')
    In [14]: f['temp']['block0_values'][:]
    Out[14]: 
    array([[  2.45772350e+06,   0.00000000e+00,   1.00000000e+00],
           [  2.45772450e+06,   2.00000000e+00,   3.00000000e+00],
           [  2.45772550e+06,   4.00000000e+00,   5.00000000e+00],
           [  2.45772650e+06,   6.00000000e+00,   7.00000000e+00],
           [  2.45772750e+06,   8.00000000e+00,   9.00000000e+00],
           [  2.45772850e+06,   1.00000000e+01,   1.10000000e+01],
           [  2.45772950e+06,   1.20000000e+01,   1.30000000e+01],
           [  2.45773050e+06,   1.40000000e+01,   1.50000000e+01],
           [  2.45773150e+06,   1.60000000e+01,   1.70000000e+01],
           [  2.45773250e+06,   1.80000000e+01,   1.90000000e+01]])
    

    所以它已经将索引信息保存在 3 个系列中,并将值保存在另一个系列中,加载为 2d numpy 数组。

    这与我希望从pytables 创建的文件中看到的信息相同。

    根据它的文档,pd.HDFStore 正在使用pytables

    【讨论】:

    • 是的,我在问题中提到我使用 pytables,实际上我通过 'pd.HDFStore' 函数使用 pytables。与@piRSquared 提供的解决方案几乎相同。但我真正的查询是,如果我改为使用 h5py,因为我在此过程中丢失了日期 dtype 信息,我无法查询我的对象 - 比如说,基于日期范围 - 就像我可以使用 pd.HDFStore 一样。
    • 我通过h5py 得到的是数组。我没有显示组和系列的attrs。大概使用h5py,我可以阅读重新创建与源大致匹配的DataFrame所需的所有内容,并允许我根据需要使用queryHDFStore 负责处理您在使用 h5py 时必须明确处理的细节。
    猜你喜欢
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多