简体   繁体   中英

Matplotlib/numpy/pandas IndexError: index 0 is out of bounds for axis 0 with size 0

I am fairly new to numpy and pandas and have been encountering the following error I have no idea how to fix. I am getting this error even though the array shape is (2603, 1).

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

This is causing me many problems in scaling the data and plotting it. I got the data from: http://bitcoincharts.com/charts/chart.json?m=bitstampUSD#rg360zig12-hourztgSzm1g10zm2g25zv containing Bitcoin history.

Here is my code:

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)

ADDITION

I solved the error by filling NaN values with a number (I did the mean of the previous 5 rows).

So the problem is in this line

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

You are setting an index of the dataFrame as timeStamp which is not 0. Matplotlib searches for the index 0 which shows the error.

You could reindex it again from 0 to n-1 then you will get the desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM