Search code examples
pythonpandasnumpymatplotlibindex-error

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).


Solution

  • 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.