5

我正在尝试 ptrepack 使用 pandas HDFStore pytables 接口创建的 HDF 文件。数据框的主要索引是时间,但我制作了更多列data_columns,以便我可以通过这些 data_columns 过滤磁盘上的数据。

现在我想按这些列之一对 HDF 文件进行排序(因为选择太慢了,84 GB 文件不适合我的口味),使用 ptrepack 和如下sortby选项:

()[maye@luna4 .../nominal]$ ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc --sortby=clat C9.h5 C9_sorted.h5

我收到错误消息:

()[maye@luna4 .../nominal]$ 从 'C9.h5:/' 复制到 'C9_sorted.h5:/' 时出现问题 错误是 --> : Field clatmust have associated a 'full' index in表/df/table (Table(390557601,)) ''。目标文件如下所示: C9_sorted.h5 (File) '' Last modif.: 'Fri Jul 26 18:17:56 2013' Object Tree: / (RootGroup) '' /df (Group) '' /df/table (表(0,),洗牌,blosc(9))''

回溯(最近一次调用):文件“/usr/local/epd/bin/ptrepack”,第 10 行,在 sys.exit(main()) 文件“/usr/local/epd/lib/python2.7/site -packages/tables/scripts/ptrepack.py”,第 480 行,在主要 upgradeflavors=upgradeflavors) 文件“/usr/local/epd/lib/python2.7/site-packages/tables/scripts/ptrepack.py”,行225、在copyChildren raise RuntimeError("请检查节点名是否" RuntimeError: 请检查节点名是否在destination中不重复,如果有,如果需要,添加--overwrite-nodes标志。特别是支付请注意,rootUEP 不会欺骗您。

这是否意味着我不能按索引列对 HDF 文件进行排序,因为它们不是“完整”索引?

4

2 回答 2

10

我已经测试了 Jeff 在我们上面的闲聊中提到的几个选项。

请看一下这个笔记本,希望它能帮助你为你的数据存储做出相关的决定:https ://nbviewer.ipython.org/810bd0720bb1732067ff 笔记本的要点在这里:https ://gist.github.com /michaelaye/810bd0720bb1732067ff

我的主要结论:

  • 使用 index=False 有几个令人印象深刻的效果:
    1. 它减小了生成的 HDF 文件的文件大小。
    2. 它创建 HDF 文件的速度要快得多。
    3. 即使 ptdump 和 storer().group.table 打印输出没有显示任何索引,商店显示仍然显示索引器和数据列(这可能是我对 pytables 机器的无知)。
  • 通过 store.create_table_index() 创建索引对通过其中一个数据列选择数据的速度没有任何帮助。
  • 该索引必须是“完整”索引,以便后面带有 --sortby 的 ptrepack 不会保释但它不一定索引级别 9。默认级别 6 很好,并且似乎不会显着影响数据选择速度。也许它会有很多列?
  • 使用 --propindexes 几乎使 ptrepacking 时间翻倍,数据选择速度略有提高。
  • 使用压缩和 --propindexs 只比单独使用 --propindex 慢一点,而数据大小(至少在这个例子中)并没有显着下降。
  • 使用压缩后,数据选择速度似乎没有太大差异。
  • 1 mio 这个例子的加速。在对选择列进行排序后,仅使用 --sortby 而不使用 --propindexes 的 2 列随机数据行约为 5。

为了完成,命令的超短摘要:

df = pd.DataFrame(randn(1e6,2),columns=list('AB')).to_hdf('test.h5','df',
                  data_columns=list('AB'),mode='w',table=True,index=False)
store = pd.HDFStore('test.h5')
store.create_table_index('df',columns=['B'], kind='full')
store.close()

在外壳中:

ptrepack --chunkshape=auto --sortby=B test.h5 test_sorted.h5
于 2013-07-31T01:00:08.627 回答
7

这是一个完整的例子。

使用 data_column 创建框架。将索引重置为完整索引。使用 ptrepack 对其进行排序。

In [16]: df = DataFrame(randn(10,2),columns=list('AB')).to_hdf('test.h5','df',data_columns=['B'],mode='w',table=True)

In [17]: store = pd.HDFStore('test.h5')

In [18]: store
Out[18]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df            frame_table  (typ->appendable,nrows->10,ncols->2,indexers->[index],dc->[B])

In [19]: store.get_storer('df').group.table
Out[19]: 
/df/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoIndex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
    "B": Index(6, medium, shuffle, zlib(1)).is_CSI=False}

In [20]: store.create_table_index('df',columns=['B'],optlevel=9,kind='full')

In [21]: store.get_storer('df').group.table
Out[21]: 
/df/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoIndex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_CSI=False,
    "B": Index(9, full, shuffle, zlib(1)).is_CSI=True}

 In [22]: store.close()

 In [25]: !ptdump -avd test.h5
/ (RootGroup) ''
  /._v_attrs (AttributeSet), 4 attributes:
   [CLASS := 'GROUP',
    PYTABLES_FORMAT_VERSION := '2.0',
    TITLE := '',
    VERSION := '1.0']
/df (Group) ''
  /df._v_attrs (AttributeSet), 14 attributes:
   [CLASS := 'GROUP',
    TITLE := '',
    VERSION := '1.0',
    data_columns := ['B'],
    encoding := None,
    index_cols := [(0, 'index')],
    info := {'index': {}},
    levels := 1,
    nan_rep := b'nan',
    non_index_axes := [(1, ['A', 'B'])],
    pandas_type := b'frame_table',
    pandas_version := b'0.10.1',
    table_type := b'appendable_frame',
    values_cols := ['values_block_0', 'B']]
/df/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoindex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "B": Index(9, full, shuffle, zlib(1)).is_csi=True}
  /df/table._v_attrs (AttributeSet), 15 attributes:
   [B_dtype := b'float64',
    B_kind := ['B'],
    CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'values_block_0',
    FIELD_2_FILL := 0.0,
    FIELD_2_NAME := 'B',
    NROWS := 10,
    TITLE := '',
    VERSION := '2.6',
    index_kind := b'integer',
    values_block_0_dtype := b'float64',
    values_block_0_kind := ['A']]
  Data dump:
[0] (0, [1.10989047288066], 0.396613633081911)
[1] (1, [0.0981650001268093], -0.9209780702446433)
[2] (2, [-0.2429293157073629], -1.779366453624283)
[3] (3, [0.7305529521507728], 1.243565083939927)
[4] (4, [-0.1480724789512519], 0.5260130757651649)
[5] (5, [1.2560020435792643], 0.5455842491255144)
[6] (6, [1.20129355706986], 0.47930635538027244)
[7] (7, [0.9973598999689721], 0.8602929579025727)
[8] (8, [-0.40070941088441786], 0.7622228032635253)
[9] (9, [0.35865804118145655], 0.29939126149826045)

这是另一种创建完全排序索引的方法(而不是这样写)

In [23]: !ptrepack --sortby=B test.h5 test_sorted.h5

In [26]: !ptdump -avd test_sorted.h5
/ (RootGroup) ''
  /._v_attrs (AttributeSet), 4 attributes:
   [CLASS := 'GROUP',
    PYTABLES_FORMAT_VERSION := '2.1',
    TITLE := '',
    VERSION := '1.0']
/df (Group) ''
  /df._v_attrs (AttributeSet), 14 attributes:
   [CLASS := 'GROUP',
    TITLE := '',
    VERSION := '1.0',
    data_columns := ['B'],
    encoding := None,
    index_cols := [(0, 'index')],
    info := {'index': {}},
    levels := 1,
    nan_rep := b'nan',
    non_index_axes := [(1, ['A', 'B'])],
    pandas_type := b'frame_table',
    pandas_version := b'0.10.1',
    table_type := b'appendable_frame',
    values_cols := ['values_block_0', 'B']]
/df/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  /df/table._v_attrs (AttributeSet), 15 attributes:
   [B_dtype := b'float64',
    B_kind := ['B'],
    CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'values_block_0',
    FIELD_2_FILL := 0.0,
    FIELD_2_NAME := 'B',
    NROWS := 10,
    TITLE := '',
    VERSION := '2.6',
    index_kind := b'integer',
    values_block_0_dtype := b'float64',
    values_block_0_kind := ['A']]
  Data dump:
[0] (2, [-0.2429293157073629], -1.779366453624283)
[1] (1, [0.0981650001268093], -0.9209780702446433)
[2] (9, [0.35865804118145655], 0.29939126149826045)
[3] (0, [1.10989047288066], 0.396613633081911)
[4] (6, [1.20129355706986], 0.47930635538027244)
[5] (4, [-0.1480724789512519], 0.5260130757651649)
[6] (5, [1.2560020435792643], 0.5455842491255144)
[7] (8, [-0.40070941088441786], 0.7622228032635253)
[8] (7, [0.9973598999689721], 0.8602929579025727)
[9] (3, [0.7305529521507728], 1.243565083939927)
于 2013-07-27T12:52:46.893 回答