python - 前一个值大于等于当前值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)
解决方案
你正在做 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)
推荐阅读
- react-native - 无效的解构不可交互实例的尝试
- c# - 有没有一种从结构中获取随机值的有效方法?
- git - “git log --follow”总是忽略合并提交
- python - 如何在 Python 中捕获特定的索引错误并为此附加新值?
- javascript - 是否可以访问表外的 BTable 道具?
- google-sheets - 跨工作表的条件格式以基于大于比较突出显示单元格
- node.js - 如何在打字稿中合并来自不同文件的不同快递路线
- python - 具有图像尺寸的预测时的 Faster-RCNN Pytorch 问题
- ios - 将图像文件保存在文档目录中并从中获取图像文件并在 Objective C 中的视图上显示
- javascript - 当我使用 app.js 中的模板时,当我使用 zepto.js(甚至是 jquery)时,我无法通过 ajax 连接 php 代码