Home > database >  Unsupported operand type(s) for : 'int' and 'datetime.timedelta'
Unsupported operand type(s) for : 'int' and 'datetime.timedelta'

Time:12-03

Am getting this error, while trying to plot a chart. I am trying to plot a chart using the values provided. I am using yahoo finance and want to plot a resistance line using 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")

this is the whole code

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 : ")

The error I am receiving is

Traceback (most recent call last): File "Low.py", line 57, in

plt.plot_date([dates[index],dates[index] timeD,
TypeError: unsupported operand type(s) for  : 'int' and 'datetime.timedelta'

Thanks

CodePudding user response:

The problem is with this expression in the plt.plot_date:

dates[index] timeD

If you print dates you will get this output:

[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')]

As you can see the list contains Int and Timestamp values. So when you add timeD witch is a timedelta this operation is illegal.

I don't understand how you construct dates but you need to go over the logic in that section and check why you are getting Int and Timestamp values.

  • Related