首页 > 解决方案 > 前一个值大于等于当前值1.2倍的索引

问题描述

对于任何给定的日期,我都试图找到close比现值高 1.2 倍的先前close值。我做了一个循环来检查每一行。但是,它效率不高。运行时间为 45 秒。如何使我的代码更有效地处理比这更大的数据集?

数据集 - TSLA 或TSLA Daily 5Y Stock Yahoo

df = pd.read_csv(os.getcwd()+"\\TSLA.csv")

# Slicing the dataset
df2 = df[['Date', 'Close']]

irange = np.arange(1, len(df))

for i in irange:
      # Dicing first i rows
      df3 = df2.head(i)
      # Set the target close value that is 1.2x the current close value
      targetValue = 1.2 * df3['Close'].tail(1).values[0]

      # Check the last 200 days
      df4 = df3.tail(200)
      df4.set_index('Date', inplace=True)

      # Save all the target values in a list
      req = df4[df4['Close'] > targetValue]
      try:
          lent = (req.index.tolist()[-1])
      except:
          lent = str(9999999)

      # Save the last value to the main dataframe
      df.at[i,'last_time'] = lent

df.tail(20)

标签: pythonpandas

解决方案


你正在做 O(N^3) 和一些不必要的数据副本。试试这种 O(NlogN) 方式

df = pd.read_csv("D:\\TSLA.csv")
stack,cnt=[],0
def OnePointTwoTimesLarger(row):
    #cnt is not really needed by what you aksed. But it is usually a better to return the data row you need, instead of just returning the value
    global stack,cnt
    c=row['Close']
    while stack and stack[-1][1]<=c:
        stack.pop()
    stack.append([row['Date'],c])
    cnt+=1
    left,right=0,len(stack)-1
    while left<right-3:
        mid=(left+right)//2
        if stack[mid][1]>1.2*c:
            left=mid
        else:
            right=mid
    for e in stack[left:right+1][::-1]:
        if e[1]>1.2*c:
            return e[0]
    return 999999
df['last_time']=df.apply(OnePointTwoTimesLarger, axis=1)
df.tail(60)

推荐阅读