2

PyTables ライブラリと HDFStore オブジェクト (PyTables に基づく) はどちらも、ユーザーにインデックスを提供します。

PyTables だけの場合、次のように HDF5 ファイルを作成します (ドキュメントから)。

from tables import *

class Particle(IsDescription):
    identity = StringCol(itemsize=22, dflt=" ", pos=0)  # character String
    idnumber = Int16Col(dflt=1, pos = 1)  # short integer
    speed    = Float32Col(dflt=1, pos = 2)  # single-precision

# Open a file in "w"rite mode
fileh = open_file("objecttree.h5", mode = "w")

# Get the HDF5 root group
root = fileh.root

# Create the groups
group1 = fileh.create_group(root, "group1")
group2 = fileh.create_group(root, "group2")

# Now, create an array in root group
array1 = fileh.create_array(root, "array1", ["string", "array"], "String array")

# Create 1 new tables in group1
table1 = fileh.create_table(group1, "table1", Particle)

# Get the record object associated with the table:
row = table1.row

# Fill the table with 10 records
for i in xrange(10):
    # First, assign the values to the Particle record
    row['identity']  = 'This is particle: %2d' % (i)
    row['idnumber'] = i
    row['speed']  = i * 2.

    # This injects the Record values
    row.append()

# Flush the table buffers
table.flush()

# Finally, close the file (this also will flush all the remaining buffers!)
fileh.close()

ユーザーは「Column.create_index()」を使用して列にインデックスを付けます

例えば:

indexrows = table.cols.var1.create_index() 
indexrows = table.cols.var2.create_index() 
indexrows = table.cols.var3.create_index()

後者の場合、ユーザーは HDFStore オブジェクトをインスタンス化してから、インデックスを作成する列を選択します。

store = HDFStore('file1.hd5')
key = "key_name"
index_columns = ["column1", "column2"]
store.append(key,... data_columns=index_columns)

ここでは、検索を最適化する 2 つの列にインデックスを付けます。

2 つの質問:

(1)実際には、PyTablesの例(最初の例)でインデックス(インデックス)を設定する方法が明確ではありません。上記で定義された列はありません。私の考えでは、identity、idnumber、speed の 3 つのフィールドがあります。速度とアイデンティティにインデックスを付けたいとしましょう。どうすればこれを行うことができますか?

(2) パンダ ベースのインデックス作成と PyTables ベースのインデックス作成の間にベンチマークはありますか? 一方は他方より速いですか?一方が他方よりも多くのディスク容量を占有しますか (つまり、より大きな HDF5 ファイル)?

4

0 に答える 0