Pandas/PyTables中的索引和数据列

10

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

我对Pandas HDF5 IO中的Data columns概念感到困惑,而且在谷歌上也几乎找不到相关信息。由于我正在涉足涉及HDF5存储的大型项目中,因此我希望能够清楚地了解这些概念。
文档中说:
您可以指定(并索引)要执行查询的某些列(除了可索引列,您始终可以查询)。例如,假设您要在磁盘上执行此常见操作,并仅返回与此查询匹配的框架。您可以将data_columns=True指定为强制所有列都是数据列。
这很令人困惑:
  1. other than the indexable columns, which you can always query: “可索引”列是什么?难道不是所有的列都可以被索引吗?这个术语是什么意思?

  2. 例如,假设您想要在磁盘上执行此常见操作,并仅返回与此查询匹配的框架。那么这与在Pytable上进行正常查询有何不同;无论是否有任何索引?

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


我也遇到了同样的问题,使用HDFStore.select_column函数时出现了问题。在弄清楚需要在data_columns中设置列之后才发现了这个问题。这个github问题进一步深入了解:https://github.com/pandas-dev/pandas/issues/21188 - Nikhil VJ
1个回答

8
你应该尝试一下。
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参数,则存储的帧的索引被自动设置为可查询列。默认情况下,无法查询其他列。

  • 如果指定了data_columns=Truedata_columns=list_of_columns,则这些列将被单独存储,并且随后可以查询。

  • 如果指定了index=False,则对于可查询列(例如index和/或data_columns),将不会自动创建PyTables索引。

要查看实际创建的索引(即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存储在块中(速度更快/占用空间较少)。

通常,您总是希望对要检索的列进行索引,但是,如果您正在创建并将多个文件附加到单个存储中,则通常关闭索引创建,并在最后执行它(因为逐步创建索引非常昂贵)。

有关各种问题,请参见食谱


设置index=True有什么用?在我看来,我可以设置data_columns=True,index=False,仍然可以使用表的列查询表。 - Michael
当然可以,但实际上你不会从索引中获得任何查询的线性扫描的好处。当进行追加操作时,index=False非常有用,例如,多次追加然后构建索引比带有索引的追加更有效(对于大量数据)。 - Jeff
我在完成附加后如何构建索引?我在pandas文档中找不到示例?谢谢你的帮助。 - Michael
1
请查看此帖子:https://dev59.com/n3TYa4cB1Zd3GeqP0vbo - Jeff
是的,带有迷你示例的问题。 - Jeff
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接