大于或等于当前值1.2倍的上一个值的索引

2024-06-26 17:44:21 发布

您现在位置:Python中文网/ 问答频道 /正文

对于任何给定的日期,我都试图找到前一个close值,该值是当前close值的1.2倍。我做了一个循环,将检查每一行。然而,这是没有效率的。运行时间为45秒。如何使我的代码更高效地处理比这大得多的数据集

数据集-TSLATSLA 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)

Tags: csvthe数据dfclosedatevaluetail
1条回答
网友
1楼 · 发布于 2024-06-26 17:44:21

您正在执行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)

相关问题 更多 >