我已经确定了一个熊猫司令部
timeseries.loc[z, x] = y
对迭代中花费的大部分时间负责。现在我正在寻找更好的方法来加速它。循环覆盖的元素甚至不到5万个(生产目标是25万个或更多),但已经需要20秒的时间。
这是我的代码(忽略上半部分,它只是计时助手)
def populateTimeseriesTable(df, observable, timeseries):
"""
Go through all rows of df and
put the observable into the timeseries
at correct row (symbol), column (tsMean).
"""
print "len(df.index)=", len(df.index) # show number of rows
global bf, t
bf = time.time() # set 'before' to now
t = dict([(i,0) for i in range(5)]) # fill category timing with zeros
def T(i):
"""
timing helper: Add passed time to category 'i'. Then set 'before' to now.
"""
global bf, t
t[i] = t[i] + (time.time()-bf)
bf = time.time()
for i in df.index: # this is the slow loop
bf = time.time()
sym = df["symbol"][i]
T(0)
tsMean = df["tsMean"][i]
T(1)
tsMean = tsFormatter(tsMean)
T(2)
o = df[observable][i]
T(3)
timeseries.loc[sym, tsMean] = o
T(4)
from pprint import pprint
print "times needed (total = %.1f seconds) for each command:" % sum(t.values())
pprint (t)
return timeseries
有(不重要,不慢)
def tsFormatter(ts):
"as human readable string, only up to whole seconds"
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(ts))
是的。 .
-->;要优化的代码处于for循环中。
(T和T只是辅助函数&dict,用于计时。)
我每一步都计时。绝大多数时间:
len(df.index)= 47160
times needed (total = 20.2 seconds) for each command:
{0: 1.102,
1: 0.741,
2: 0.243,
3: 0.792,
4: 17.371}
在最后一步中花费
timeseries.loc[sym, tsMean] = o
我已经下载并安装了pypy-但遗憾的是,这还不支持熊猫。
有什么办法可以加快二维数组的填充速度吗?
谢谢!
编辑:抱歉,没有提到-“timeseries”也是一个数据帧:
timeseries = pd.DataFrame({"name": titles}, index=index)
我总是认为^{} 是最快的,但不是。^{} 更快:
编辑:
我试着^{}
df
,结果是random.randint
功能不同:如果要在循环中添加行,请考虑性能问题;对于大约前1000到2000条记录,“my_df.loc”的性能更好,并且通过增加循环中的记录数而逐渐变慢。
如果你打算在一个大循环(比如说10M记录)中做一些事情,最好使用“iloc”和“append”的混合;用iloc填充一个临时数据帧,直到大小达到1000左右,然后将其附加到原始数据帧,并empy临时数据帧。这将使你的表现提高10倍左右
更新:从Pandas 0.20.1the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers开始。
一、二、二、三、三、四、四、四、四、四、四、四、四、六、六
@jezrael提供了一个有趣的比较,我决定使用更多的索引方法和10M行DF(实际上在这种特殊情况下大小并不重要)来重复它:
设置:
时间安排:
结果作为条形图:
定时数据为DF:
绘图
相关问题 更多 >
编程相关推荐