我可以矢量化这个Python Pandas代码吗?

时间:2017-02-09 05:09:36

标签: python python-2.7 pandas vectorization

我正在尝试创建一个以数据帧作为参数的函数,并返回一个符合阈值y的股票价格跳跃列表,超过一个预测期x。我想将这些观察结果放到另一个数据框中(现在,我只是将每个股票价格'跳转到列表中),但我无法在合理的数量内运行此代码我使用的数据帧的时间是4000columnsx3000rows。有没有办法对这些for循环进行矢量化以加速程序?感谢。

def jumpFilter(df, y=.1,x=1):
    lastPrice=1000 #initialization
    listOfJumps=[] #ticker, date, jump threshold passed, lookahead period, pos/neg, percentJump, avgdailyrateofchange, counter
    y=.2 #threshold-percent change in stock price
    x=1 #lookahead period
    for i in range(1,len(df.columns)):
        counter=0 #counts how many days of consistent jumps have been occurring
        for j in range(len(df.values[:])):
            if (j+x)<len(df.values):
                if type(df.values[j][i])!=str:
                    if math.isnan(float(df.values[j][i]))==False:
                        if df.values[j][i]!=0:
                            currentPrice=df.values[j][i]
                            futurePrice=df.values[j+x][i]
                            if futurePrice>currentPrice:
                                posneg=1
                            elif futurePrice<currentPrice:
                                posneg=0
                            percentJump=abs((currentPrice-futurePrice)/currentPrice)
                            if (currentPrice-futurePrice)/currentPrice>=y:
                                counter+=1
                                avgDailyRateChange=percentJump/(x+counter-1)
                            elif (currentPrice-futurePrice)/currentPrice<y and counter>=1:
                                listOfJumps.append([df.columns[i],df.values[j][0], y, x, posneg, percentJump, avgDailyRateChange, counter])
                                counter=0
    return listOfJumps

0 个答案:

没有答案