python - TypeError 的问题:无法使用非整数键按位置索引进行索引
问题描述
我在下面的代码中遇到了这一行的问题,它抛出
TypeError: Cannot index by location index with a non-integer key
如何更改代码?
exit_price = df.loc[exit_time + dt.timedelta(minutes=1)].Open
import yfinance as yf
import pandas as pd
import datetime as dt
asset = "TSLA"
intraday = yf.download(asset, start = "2021-03-17", end = "2021-03-18", interval = "1m")
def Intradaytrend(df, entry, exit):
ret_120min = df.iloc[120].Open/df.iloc[0].Open -1
tickret = df.Open.pct_change()
if ret_120min > entry:
buyprice = df.iloc[121].Open
buytime = df.iloc[121].name
cumulated = (tickret.loc[buytime:] +1).cumprod() -1
exittime = cumulated[(cumulated < -exit) | (cumulated > exit)].first_valid_index()
if exittime == None:
exitprice = df.iloc[-1].Open
else:
exitprice = df.iloc[exittime + dt.timedelta(minutes=1)].Open
profit = exitprice - buyprice
profitrel = profit/buyprice
return profitrel
else:
return None
Intradaytrend(intraday, 0.01, 0.01)
解决方案
推荐阅读
- angular - (Angular 6)让数据库中的 Itens 项目
- junit - 在 Kotlin 和 JUnit 5 中重复使用 @ExtendWith
- go - dial tcp remote_ip:6379: connect: 连接超时
- python - SQLAlchemy:如何执行多个独立的 CTE?
- java - 如何在 Mac 上将 SSL 证书 .pem 文件导入 JRE cacerts 文件?
- java - 在并发线程中使用 UserTransaction
- zoho - 电子商务推荐程序的 Zoho 数据结构设置
- reactjs - 反应 contextType 没有获取上下文值
- java - 如何使用 json 对象和 jsonarray
- excel - 打开工作簿时运行宏