python - Pandas HDFStore.create_table_index 没有提高选择查询速度,寻找更好的搜索方式

标签 python pandas hdfstore

我已经创建了一个 HDFStore。 HDFStore 包含一个 df 组,它是一个包含 2 列的表。 第一列是 string,第二列是 DateTime(将按排序顺序排列)。 商店是使用以下方法创建的:

from numpy import ndarray
import random
import datetime
from pandas import DataFrame, HDFStore


def create(n):
    mylist = ['A' * 4, 'B' * 4, 'C' * 4, 'D' * 4]
    data = []
    for i in range(n):
        data.append((random.choice(mylist),
                     datetime.datetime.now() - datetime.timedelta(minutes=i)))

    data_np = ndarray(len(data), dtype=[
                      ('fac', 'U6'), ('ts', 'datetime64[us]')])
    data_np[:] = data
    df = DataFrame(data_np)
    return df


def create_patches(n, nn):
    for i in range(n):
        yield create(nn)


df = create_patches(100, 1000000)
store = HDFStore('check.hd5')
for each in df:
    store.append('df', each, index=False, data_columns=True, format = 'table')
store.close()

创建 HDF5 文件后,我将使用以下方法查询表:

In [1]: %timeit store.select('df', ['ts>Timestamp("2016-07-12 10:00:00")'])
1 loops, best of 3: 13.2 s per loop

所以,基本上这需要 13.2 秒,然后我使用

向该列添加了一个索引
In [2]: store.create_table_index('df', columns=['ts'], kind='full')

然后我又做了同样的查询,这次我得到了以下信息:-

In [3]: %timeit store.select('df', ['ts>Timestamp("2016-07-12 10:00:00")'])
1 loops, best of 3: 12 s per loop

从上面的内容来看,在我看来,性能并没有显着提高。所以,我的问题是,在这里我还能做些什么来加快查询速度,还是我做错了什么?

最佳答案

我认为当您指定 data_columns=True 时您的列已经被编入索引...

查看此演示:

In [39]: df = pd.DataFrame(np.random.randint(0,100,size=(10, 3)), columns=list('ABC'))

In [40]: fn = 'c:/temp/x.h5'

In [41]: store = pd.HDFStore(fn)

In [42]: store.append('table_no_dc', df, format='table')

In [43]: store.append('table_dc', df, format='table', data_columns=True)

In [44]: store.append('table_dc_no_index', df, format='table', data_columns=True, index=False)

未指定data_columns,因此仅索引索引:

In [45]: store.get_storer('table_no_dc').group.table
Out[45]:
/table_no_dc/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Int32Col(shape=(3,), dflt=0, pos=1)}
  byteorder := 'little'
  chunkshape := (3276,)
  autoindex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False}

data_columns=True - 所有数据列都已编入索引:

In [46]: store.get_storer('table_dc').group.table
Out[46]:
/table_dc/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "A": Int32Col(shape=(), dflt=0, pos=1),
  "B": Int32Col(shape=(), dflt=0, pos=2),
  "C": Int32Col(shape=(), dflt=0, pos=3)}
  byteorder := 'little'
  chunkshape := (3276,)
  autoindex := True
  colindexes := {
    "C": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "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}

data_columns=True, index=False - 我们有数据列信息,但没有索引:

In [47]: store.get_storer('table_dc_no_index').group.table
Out[47]:
/table_dc_no_index/table (Table(10,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "A": Int32Col(shape=(), dflt=0, pos=1),
  "B": Int32Col(shape=(), dflt=0, pos=2),
  "C": Int32Col(shape=(), dflt=0, pos=3)}
  byteorder := 'little'
  chunkshape := (3276,)

colindexes - 显示上述示例中的索引列列表

关于python - Pandas HDFStore.create_table_index 没有提高选择查询速度,寻找更好的搜索方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38437413/

相关文章:

python - 使用不区分大小写的方式从行名中选择数据帧行(如 `grep -i` )

python - 使用 pandas.HDFStore 读取 HDF5 文件中的整个组

python - 具有按索引值选择多索引的 Pandas HDFStore 找不到列

python - 如何将数据 append 到存储在 HDFStore 文件中的面板

python - df1 中不在 df2 中的所有行

python - 从两个列表制作字典

python - 将非空值向前传播到最后一个条目

python - 获取带有空日期的行 pandas python

python - numpy 返回指数的 nans。纯 Python 返回正确的结果

python - 计算两个矩形之间的重叠面积