您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:21:57 python 字数 5273 阅读 525 来源 www.jb51.cc/python

我创建了一个HDFStore.HDFStore包含一个组df,它是一个包含2列的表.第一列是字符串,第二列是DateTime(将按排序顺序).已使用以下方法创建商店:from numpy import ndarray import random import datetime from pandas import DataFrame, HDFStore

概述

我创建了一个hdfstore.
hdfstore包含一个组df,它是一个包含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',best of 3: 12 s per loop

从上面看,在我看来,性能没有显着改善.所以,我的问题是,我还能做些什么来加快查询速度,或者我做错了什么?

看这个演示:

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',format='table',data_columns=True)

In [44]: store.append('table_dc_no_index',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,),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,"A": Int32Col(shape=(),pos=1),"B": Int32Col(shape=(),pos=2),"C": Int32Col(shape=(),pos=3)}
  byteorder := 'little'
  chunkshape := (3276,)
  autoindex := True
  colindexes := {
    "C": Index(6,zlib(1)).is_csi=False,"A": Index(6,"index": Index(6,"B": Index(6,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,)

colindexes – 显示上面示例中的索引列的列表

总结

以上是编程之家为你收集整理的python – Pandas HDFStore.create_table_index不提高选择查询速度,寻找更好的搜索方式全部内容,希望文章能够帮你解决python – Pandas HDFStore.create_table_index不提高选择查询速度,寻找更好的搜索方式所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶