TypeError解决方案

时间:2019-10-06 21:28:13

标签: python

尝试使用一些数据运行此代码,并不断给出错误。关于如何解决该问题的任何想法?预先感谢:

import pandas as pd
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
import datetime

#Import our historical data
data = pd.read_csv('Data/EURUSD.csv')
data.columns = [['Date', 'open', 'high', 'low', 'close', 'vol']]
data = data.drop_duplicates(keep=False)
data.Date = pd.to_datetime(data.Date,format='%d.%m.%Y %H:%M:%S.%f')
data = data.set_index(data.Date)
data = data[['open', 'high', 'close', 'vol']]

price = data.close.iloc[:100]

# Find our relative extrema
max_idx = argrelextrema(price.values,np.greater,order=1)
min_idx = argrelextrema(price.values,np.less,order=1)

print(max_idx)
print(min_idx)

https://pastebin.com/sKqq2PgD

TypeError:仅整数标量数组可以转换为标量索引

1 个答案:

答案 0 :(得分:1)

您访问字段的方式不正确。

例如代替

pd.to_datetime(data.Date,format='%d.%m.%Y %H:%M:%S.%f')

您应该

 pd.to_datetime(data.iloc(0)[0].Date, format='%d.%m.%Y %H:%M:%S.%f')

作为旁注:您在pastebin中发布的数据是制表符分隔的。您问题中的代码应更改为data = pd.read_csv('Data/EURUSD.csv', sep='\t')