gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for +: 'float' and 'datetime.timedelta'

转载 作者:行者123 更新时间:2023-11-28 18:36:22 27 4
gpt4 key购买 nike

我遇到了这个错误。从这一行:

ohlc[0][i+1] = ohlc[0][i] + dt.timedelta(days=1)

这里是上下文:

stock_price_url = 'https://www.quandl.com/api/v3/datasets/WIKI/AAPL/data.csv?start_date=2015-06-01&order=asc&end_date=2015-08-01&collapse=daily'
source_code = urllib.urlopen(stock_price_url).read().decode()
stock_data = []
split_source = source_code.split('\n')
for line in split_source:
split_line = line.split(',')
if 'Date' not in line:
stock_data.append(line)


date, openp, highp, lowp, closep, volume = np.loadtxt(stock_data,
delimiter=',',
unpack=True,
converters={0:strpdate2num('%Y-%m-%d')},
usecols=(0,1,2,3,4,5))

x = 0
y = len(date)
ohlc

while x < y:
append_me = date[x], openp[x], closep[x], highp[x], lowp[x], volume[x]
ohlc.append(append_me)
x+=1

# the dates in my example file-set are very sparse (and annoying) change the dates to be sequential

for i in range(len(date)-1):
ohlc[0][i+1] = ohlc[0][i] + dt.timedelta(days=1)

试图从这个 answer 做一些类似于这行的事情:

# the dates in my example file-set are very sparse (and annoying) change the dates to be sequential
for i in range(len(r)-1):
r['date'][i+1] = r['date'][i] + datetime.timedelta(days=1)

任何帮助都会非常棒。

最佳答案

错误源于试图将两种不同的数据类型相加 - float 和日期。

从粗略的谷歌,matplotlib.dates num2date在这种情况下,功能可能会有所帮助。 datetime documentation也可能有帮助。

关于python - 类型错误 : unsupported operand type(s) for +: 'float' and 'datetime.timedelta' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32120499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com