TypeError:+的不支持的操作数类型:'float'和'datetime.timedelta'

时间:2015-08-20 14:07:28

标签: python matplotlib

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

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)

任何帮助都会非常棒。

1 个答案:

答案 0 :(得分:1)

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

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