【发布时间】:2012-08-16 16:32:03
【问题描述】:
我一直在为一些大型金融数据集测试 pandas 和 pytables,但遇到了一个真正的绊脚石:
当存储在 pytables 文件中时,pandas 似乎将多维数据存储在非常长的行中,而不是列中。
试试这个:
from pandas import *
df = DataFrame({'col1':randn(100000000),'col2':randn(100000000)})
store = HDFStore('test.h5')
store['data'] = df #should be a warning here about exceeding the maximum recommended rowsize
store.handle
输出:
File(filename=test7.h5, title='', mode='a', rootUEP='/', filters=Filters(complevel=0, shuffle=False, fletcher32=False))
/ (RootGroup) ''
/data (Group) ''
/data/axis0 (Array(2,)) ''
atom := StringAtom(itemsize=4, shape=(), dflt='')
maindim := 0
flavor := 'numpy'
byteorder := 'irrelevant'
chunkshape := None
/data/axis1 (Array(100000000,)) ''
atom := Int64Atom(shape=(), dflt=0)
maindim := 0
flavor := 'numpy'
byteorder := 'little'
chunkshape := None
/data/block0_items (Array(2,)) ''
atom := StringAtom(itemsize=4, shape=(), dflt='')
maindim := 0
flavor := 'numpy'
byteorder := 'irrelevant'
chunkshape := None
/data/block0_values (Array(2, 100000000)) ''
atom := Float64Atom(shape=(), dflt=0.0)
maindim := 0
flavor := 'numpy'
byteorder := 'little'
chunkshape := None
我不完全确定,但我认为结合错误消息,Array(2,100000000) 表示具有 2 行和 100,000,000 列的二维数组。这也是它在 HDFView 中的显示方式。
我的性能一直很差(在某些情况下 data['ticks'].head() 需要 10 秒),这是什么原因造成的?
【问题讨论】:
-
github.com/pydata/pandas/pull/1834 和 github.com/pydata/pandas/issues/1824 修复此问题。现在数据是否按列保存?就时间序列数据和列方式的保存(如 q/kdb+ 等)而言,这是最优的吗?除了 PyTables 之外,我真的找不到其他关于如何使用 HDFStore 保存数据的内容。
-
它目前以一种相当奇怪的方式存储,请尝试查看您创建的 hdf5 存储的句柄以获取更多信息,或在 HDFView 中查看它。
标签: python performance pandas hdf5 pytables