5

HDFStore を作成しました。HDFStore には、df2 つの列を持つテーブルであるグループが含まれています。最初の列は astringで、2 番目の列は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

以上のことから、パフォーマンスに大きな改善は見られないように思えます。それで、私の質問は、クエリを高速化するためにここで他に何ができるか、または何か間違っていることがありますか?

4

1 に答える 1

3

あなたが指定したときに、あなたの列はすでにインデックス化されていると思います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- 上記の例の索引付けされた列のリストを示します

于 2016-07-18T16:07:19.983 に答える