【发布时间】:2017-05-24 09:30:29
【问题描述】:
最近我一直在运行一些(似乎)数据密集型的数据分析程序。我有一个相当大的 ~ 600 万行、20 列的数据集。该程序在 Python 中主要使用 pandas 和 numpy。对数据也进行了很多操作。
在使用分析器改进代码之后,我一直在做一些事情。我在数据库表上为DataDate 列创建了一个索引,这大大提高了速度,但到目前为止,代码中的瓶颈仍然是pandas read_sql 函数。
我的程序通常只想访问整个数据集的一小部分,例如 15 行。在我看来,检索数据的大小与程序检索信息的次数之间似乎存在权衡。对于下图,您可以看到read_sql 函数花费了 761,整个程序大约花费了 790 完成。
程序只有1个子程序和1行调用read_sql:
this_trade = pandas.read_sql("""select * from options where "OptionRoot" = '%s' and "DataDate" between '%s' and '%s' order by "DataDate" asc limit 21;""" % (row['OptionRoot'],row['DataDate'],row['Expiration']), engine)
我尝试在开始时将整个options 表加载到pandas 数据帧中,然后在子例程中从数据帧访问数据:
this_trade = options[ ( options['OptionRoot'] == row['OptionRoot'] ) & (options['DataDate'].between(row['DataDate'],row['Expiration']) ) ]
进展如此缓慢,我什至不想等待分析器输出。
所以问题是:我能以某种方式加快数据读取速度吗?
【问题讨论】:
标签: python postgresql pandas profiling