Pandas / PyTables中的索引和数据列

时间:2014-09-07 20:37:53

标签: python pandas pytables

http://pandas.pydata.org/pandas-docs/stable/io.html#indexing

我对Pandas HDF5 IO中的数据列概念感到困惑。此外,谷歌搜索它的信息很少甚至没有。由于我在一个涉及HDF5存储的大型项目中潜入熊猫,我想清楚这些概念。

文档说:

  

您可以指定(并索引)您希望能够使用的某些列   执行查询(可索引列除外)   总是查询)。比如说你想要执行这个常见的事情   操作,磁盘上,并返回与此查询匹配的帧。   您可以指定data_columns = True以强制所有列   data_columns

这令人困惑:

  1. other than the indexable columns, which you can always query:什么是可索引的'列?并非所有列都可以索引'?这个术语是什么意思?

  2. For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query.与Pytable的正常查询有什么不同?是否有data_columns的任何索引?

  3. 非索引,索引和data_column列之间的根本区别是什么?

1 个答案:

答案 0 :(得分:7)

你应该尝试一下。

In [22]: df = DataFrame(np.random.randn(5,2),columns=['A','B'])

In [23]: store = pd.HDFStore('test.h5',mode='w')

In [24]: store.append('df_only_indexables',df)

In [25]: store.append('df_with_data_columns',df,data_columns=True)

In [26]: store.append('df_no_index',df,data_columns=True,index=False)

In [27]: store
Out[27]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df_no_index                     frame_table  (typ->appendable,nrows->5,ncols->2,indexers->[index],dc->[A,B])
/df_only_indexables              frame_table  (typ->appendable,nrows->5,ncols->2,indexers->[index])          
/df_with_data_columns            frame_table  (typ->appendable,nrows->5,ncols->2,indexers->[index],dc->[A,B])

In [28]: store.close()
  • 您自动将存储帧的索引作为可查询列。默认情况下,不能查询其他列。

  • 如果您指定data_columns=Truedata_columns=list_of_columns,则会单独存储这些内容,然后可以随后查询。

  • 如果指定index=False,则不会为可查询列自动创建PyTables索引(例如index和/或data_columns)。< / p>

要查看正在创建的实际索引(PyTables索引),请参阅下面的输出。 colindexes定义哪些列创建了实际的PyTables索引。 (我在某种程度上截断了它。)

/df_no_index/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "A": Float64Col(shape=(), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  /df_no_index/table._v_attrs (AttributeSet), 15 attributes:
   [A_dtype := 'float64',
    A_kind := ['A'],
    B_dtype := 'float64',
    B_kind := ['B'],
    CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'A',
    FIELD_2_FILL := 0.0,
    FIELD_2_NAME := 'B',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer']
/df_only_indexables/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoindex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
  /df_only_indexables/table._v_attrs (AttributeSet), 11 attributes:
   [CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'values_block_0',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer',
    values_block_0_dtype := 'float64',
    values_block_0_kind := ['A', 'B']]
/df_with_data_columns/table (Table(5,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "A": Float64Col(shape=(), dflt=0.0, pos=1),
  "B": Float64Col(shape=(), dflt=0.0, pos=2)}
  byteorder := 'little'
  chunkshape := (2730,)
  autoindex := True
  colindexes := {
    "A": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "B": Index(6, medium, shuffle, zlib(1)).is_csi=False}
  /df_with_data_columns/table._v_attrs (AttributeSet), 15 attributes:
   [A_dtype := 'float64',
    A_kind := ['A'],
    B_dtype := 'float64',
    B_kind := ['B'],
    CLASS := 'TABLE',
    FIELD_0_FILL := 0,
    FIELD_0_NAME := 'index',
    FIELD_1_FILL := 0.0,
    FIELD_1_NAME := 'A',
    FIELD_2_FILL := 0.0,
    FIELD_2_NAME := 'B',
    NROWS := 5,
    TITLE := '',
    VERSION := '2.7',
    index_kind := 'integer']

因此,如果您要查询列,请将其设为data_column。如果不这样,那么它们将按dtype(更快/更少的空间)存储在块中。

您通常希望索引列以进行检索,但是,如果您正在创建并将多个文件附加到单个商店,则通常会关闭索引创建并在最后执行(因为这对于随你创造)。

请参阅the cookbook了解一些问题。