Jef*_*eff 76
更新为使用pandas 0.13.1
1)编号http://pandas.pydata.org/pandas-docs/dev/io.html#notes-caveats.有多种方法可以做到这一点,例如让不同的线程/进程写出计算结果,然后将一个进程组合起来.
2)根据您存储的数据类型,操作方式以及检索方式,HDF5可以提供更好的性能.在存储HDFStore作为一个单一的阵列,浮点数据,压缩的(换句话说,不将其存储在格式,允许用于查询),将被存储/读取惊人快.即使以表格格式存储(这会降低写入性能),也会提供相当好的写入性能.您可以查看一下这些详细的比较(这是HDFStore在引擎盖下使用的).http://www.pytables.org/,这是一张很好的照片:
(因为PyTables 2.3的查询现在被编入索引),所以perf实际上要比这更好.所以回答你的问题,如果你想要任何类型的性能,HDF5是要走的路.
写作:
In [14]: %timeit test_sql_write(df)
1 loops, best of 3: 6.24 s per loop
In [15]: %timeit test_hdf_fixed_write(df)
1 loops, best of 3: 237 ms per loop
In [16]: %timeit test_hdf_table_write(df)
1 loops, best of 3: 901 ms per loop
In [17]: %timeit test_csv_write(df)
1 loops, best of 3: 3.44 s per loop
Run Code Online (Sandbox Code Playgroud)
读
In [18]: %timeit test_sql_read()
1 loops, best of 3: 766 ms per loop
In [19]: %timeit test_hdf_fixed_read()
10 loops, best of 3: 19.1 ms per loop
In [20]: %timeit test_hdf_table_read()
10 loops, best of 3: 39 ms per loop
In [22]: %timeit test_csv_read()
1 loops, best of 3: 620 ms per loop
Run Code Online (Sandbox Code Playgroud)
这是代码
import sqlite3
import os
from pandas.io import sql
In [3]: df = DataFrame(randn(1000000,2),columns=list('AB'))
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000000 entries, 0 to 999999
Data columns (total 2 columns):
A 1000000 non-null values
B 1000000 non-null values
dtypes: float64(2)
def test_sql_write(df):
if os.path.exists('test.sql'):
os.remove('test.sql')
sql_db = sqlite3.connect('test.sql')
sql.write_frame(df, name='test_table', con=sql_db)
sql_db.close()
def test_sql_read():
sql_db = sqlite3.connect('test.sql')
sql.read_frame("select * from test_table", sql_db)
sql_db.close()
def test_hdf_fixed_write(df):
df.to_hdf('test_fixed.hdf','test',mode='w')
def test_csv_read():
pd.read_csv('test.csv',index_col=0)
def test_csv_write(df):
df.to_csv('test.csv',mode='w')
def test_hdf_fixed_read():
pd.read_hdf('test_fixed.hdf','test')
def test_hdf_table_write(df):
df.to_hdf('test_table.hdf','test',format='table',mode='w')
def test_hdf_table_read():
pd.read_hdf('test_table.hdf','test')
Run Code Online (Sandbox Code Playgroud)
当然是YMMV.