Pandas HDFStore用于对具有可变大小的集进行核外顺序读/写



我想以增量方式将数据读取和写入 hdf5 文件,因为我无法将数据放入内存中。

要读取/写入的数据是整数集。我只需要按顺序读取/写入集合。无需随机访问。就像我读 set1,然后是 set2,然后是 set3,等等。

问题是我无法按索引检索集合。

import pandas as pd    
x = pd.HDFStore('test.hf', 'w', append=True)
a = pd.Series([1])
x.append('dframe', a, index=True)
b = pd.Series([10,2])
x.append('dframe', b, index=True)
x.close()
x = pd.HDFStore('test.hf', 'r')
print(x['dframe'])
y=x.select('dframe',start=0,stop=1)
print("selected:", y)
x.close()

输出:

0     1
0    10
1     2
dtype: int64
selected: 0    1
dtype: int64

它没有选择我的第 0 组,这是{1,10}

这种方式有效。但我真的不知道这有多快。

这是否扫描整个文件以查找带有索引的行?

那将是浪费时间。

import pandas as pd
x = pd.HDFStore('test.hf', 'w', append=True, format="table", complevel=9)
a = pd.Series([1])
x.append('dframe', a, index=True)
b = pd.Series([10,2])
x.append('dframe', b, index=True)
x.close()
x = pd.HDFStore('test.hf', 'r')
print(x['dframe'])
y=x.select('dframe','index == 0')
print('selected:')
for i in y:
    print(i)
x.close()

输出:

0     1
0    10
1     2
dtype: int64
selected:
1
10

最新更新