Matplotlib / numpy / pandas IndexError:索引0超出了大小为0的轴0的范围

时间:2018-10-28 11:41:17

标签: python pandas numpy matplotlib index-error

我对numpy和pandas相当陌生,并且遇到了以下错误,我不知道如何解决。即使数组形状为(2603,1),我仍收到此错误。

IndexError: index 0 is out of bounds for axis 0 with size 0

这使我在缩放数据和绘制数据时遇到许多问题。我从http://bitcoincharts.com/charts/chart.json?m=bitstampUSD#rg360zig12-hourztgSzm1g10zm2g25zv获得了数据 包含比特币历史。

这是我的代码:

url = 'http://bitcoincharts.com/charts/chart.json?m=bitstampUSD#rg360zig12-hourztgSzm1g10zm2g25zv'
data = json.loads(requests.get(url).content)

df=pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume_btc', 'volume_curr', 'weighted_price'])
df.set_index('timestamp', inplace=True)
df.sort_index(inplace=True)

cols=df.columns
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

close = df['close'].values.reshape(-1, 1)
print(close.shape)

plt.plot(close)

添加

我通过用数字填充NaN值解决了错误(我做了前5行的平均值)。

1 个答案:

答案 0 :(得分:1)

问题出在这一行

df.set_index('timestamp', inplace=True)

您正在将dataFrame的索引设置为timeStamp,而不是0。Matplotlib搜索显示错误的索引0。

您可以再次将其从0重新索引为n-1,这样您将获得所需的输出。