【问题标题】:Most efficient way to get index of a table from HDF5从 HDF5 获取表索引的最有效方法
【发布时间】:2015-12-20 19:13:51
【问题描述】:

我有一个包含 pandas Series/DataFrame 表的 HDF5 文件。我需要获取存储在 HDF 中的键下的表的(熊猫)索引,但不一定是整个表:

我可以想到两种(实际上相同)获取索引的方法:

import pandas as pd

hdfPath = 'c:/example.h5'
hdfKey = 'dfkey'
# way 1:
with pd.HDFStore(hdfPath) as hdf:
    index = hdf[hdfKey].index

# way 2:
index = pd.read_hdf(hdfPath, hdfKey)

但是对于大约 2000 行的 pandas Series,这需要 0.6 秒:

%timeit pd.read_hdf(hdfPath, hdfKey).index
1 loops, best of 3: 605 ms per loop

有没有办法只获取 HDF 中表的索引?

【问题讨论】:

    标签: python hdf5 pytables h5py hdf


    【解决方案1】:

    HDFStore 对象有一个 select_column 方法,可以让您获取索引。请注意,它将返回一个以索引为值的系列。

    with pd.HDFStore(hdfPath) as hdf:
        index = hdf.select_column(hdfKey, 'index').values
    

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多