+ 不支持的操作数类型:'int' 和 'datetime.timedelta'
Unsupported operand type(s) for +: 'int' and 'datetime.timedelta'
我在尝试绘制图表时遇到此错误。我正在尝试使用提供的值绘制图表。我正在使用 yahoo finance 并想使用 python.
绘制阻力线
for index in range(len(pivots)):
print(str(pivots[index])+": "+str(dates[index]))
plt.plot_date([dates[index],dates[index]+timeD,
pivots[index],pivots[index]], linestyle="-", linewidth=2, marker="none")
这是全部代码
import pandas
import yfinance as yf
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
yf.pdr_override()
start= dt.datetime(2019,1,1)
now= dt.datetime.now()
stock = input("Enter the stock symbol: ")
while stock !="quit":
df=pdr.get_data_yahoo(stock, start, now)
df["Low"].plot(Label="low")
pivots=[]
dates=[]
counter=0
lastPivot=0
Range=[0,0,0,0,0,0,0,0,0,0]
dateRange=[0,0,0,0,0,0,0,0,0,0]
for i in df.index:
currentMin=min(Range, default=0)
value=round(df["Low"][i],2)
Range=Range[1:9]
Range.append(value)
dateRange=dateRange[1:9]
dateRange.append(i)
if currentMin==min(Range, default=0):
counter-=1
else:
counter=0
if counter==-5:
lastPivot=currentMin
dateloc=Range.index(lastPivot)
lastDate=dateRange[dateloc]
pivots.append(lastPivot)
dates.append(lastDate)
print()
# print(str(pivots))
# print(str(dates))
timeD=dt.timedelta(days=30)
for index in range(len(pivots)):
print(str(pivots[index])+": "+str(dates[index]))
plt.plot_date([dates[index],dates[index]+timeD,
pivots[index],pivots[index]], linestyle="-", linewidth=2, marker="none")
plt.show()
stock= input ("Enter the stock symbol : ")
我收到的错误是
回溯(最后一次调用):
文件“Low.py”,第 57 行,在
中
plt.plot_date([dates[index],dates[index]+timeD,
TypeError: unsupported operand type(s) for +: 'int' and 'datetime.timedelta'
谢谢
问题出在 plt.plot_date
中的这个表达式:
dates[index]+timeD
如果您打印日期,您将得到以下输出:
[0,
Timestamp('2019-02-08 00:00:00'),
Timestamp('2019-03-08 00:00:00'),
...
...
...
Timestamp('2021-09-20 00:00:00'),
Timestamp('2021-10-04 00:00:00')]
如您所见,该列表包含 Int 和 Timestamp 值。所以当你加上timeD
是一个timedelta这个操作是非法的。
我不明白你是如何构造的 dates
但你需要复习一下那部分的逻辑并检查你为什么会得到 Int 和 Timestamp 值。
我在尝试绘制图表时遇到此错误。我正在尝试使用提供的值绘制图表。我正在使用 yahoo finance 并想使用 python.
绘制阻力线 for index in range(len(pivots)):
print(str(pivots[index])+": "+str(dates[index]))
plt.plot_date([dates[index],dates[index]+timeD,
pivots[index],pivots[index]], linestyle="-", linewidth=2, marker="none")
这是全部代码
import pandas
import yfinance as yf
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
yf.pdr_override()
start= dt.datetime(2019,1,1)
now= dt.datetime.now()
stock = input("Enter the stock symbol: ")
while stock !="quit":
df=pdr.get_data_yahoo(stock, start, now)
df["Low"].plot(Label="low")
pivots=[]
dates=[]
counter=0
lastPivot=0
Range=[0,0,0,0,0,0,0,0,0,0]
dateRange=[0,0,0,0,0,0,0,0,0,0]
for i in df.index:
currentMin=min(Range, default=0)
value=round(df["Low"][i],2)
Range=Range[1:9]
Range.append(value)
dateRange=dateRange[1:9]
dateRange.append(i)
if currentMin==min(Range, default=0):
counter-=1
else:
counter=0
if counter==-5:
lastPivot=currentMin
dateloc=Range.index(lastPivot)
lastDate=dateRange[dateloc]
pivots.append(lastPivot)
dates.append(lastDate)
print()
# print(str(pivots))
# print(str(dates))
timeD=dt.timedelta(days=30)
for index in range(len(pivots)):
print(str(pivots[index])+": "+str(dates[index]))
plt.plot_date([dates[index],dates[index]+timeD,
pivots[index],pivots[index]], linestyle="-", linewidth=2, marker="none")
plt.show()
stock= input ("Enter the stock symbol : ")
我收到的错误是
回溯(最后一次调用): 文件“Low.py”,第 57 行,在
中plt.plot_date([dates[index],dates[index]+timeD,
TypeError: unsupported operand type(s) for +: 'int' and 'datetime.timedelta'
谢谢
问题出在 plt.plot_date
中的这个表达式:
dates[index]+timeD
如果您打印日期,您将得到以下输出:
[0,
Timestamp('2019-02-08 00:00:00'),
Timestamp('2019-03-08 00:00:00'),
...
...
...
Timestamp('2021-09-20 00:00:00'),
Timestamp('2021-10-04 00:00:00')]
如您所见,该列表包含 Int 和 Timestamp 值。所以当你加上timeD
是一个timedelta这个操作是非法的。
我不明白你是如何构造的 dates
但你需要复习一下那部分的逻辑并检查你为什么会得到 Int 和 Timestamp 值。