I am new to both python and numpy. I ran a code that I wrote and I am getting this message: 'index 0 is out of bounds for axis 0 with size 0' Without the context, I just want to figure out what this means.. It might be silly to ask this but what do they mean by axis 0 and size 0? index 0 means the first value in the array.. but I can't figure out what axis 0 and size 0 mean.
The 'data' is a text file with lots of numbers in two columns.
x = np.linspace(1735.0,1775.0,100)
column1 = (data[0,0:-1]+data[0,1:])/2.0
column2 = data[1,1:]
x_column1 = np.zeros(x.size+2)
x_column1[1:-1] = x
x_column1[0] = x[0]+x[0]-x[1]
x_column1[-1] = x[-1]+x[-1]-x[-2]
experiment = np.zeros_like(x)
for i in range(np.size(x_edges)-2):
indexes = np.flatnonzero(np.logical_and((column1>=x_column1[i]),(column1<x_column1[i+1])))
temp_column2 = column2[indexes]
temp_column2[0] -= column2[indexes[0]]*(x_column1[i]-column1[indexes[0]-1])/(column1[indexes[0]]-column1[indexes[0]-1])
temp_column2[-1] -= column2[indexes[-1]]*(column1[indexes[-1]+1]-x_column1[i+1])/(column1[indexes[-1]+1]-column1[indexes[-1]])
experiment[i] = np.sum(temp_column2)
return experiment
In
numpy
, index and dimension numbering starts with 0. Soaxis 0
means the 1st dimension. Also innumpy
a dimension can have length (size) 0. The simplest case is:I also get it if
x = np.zeros((0,5), int)
, a 2d array with 0 rows, and 5 columns.So someplace in your code you are creating an array with a size 0 first axis.
When asking about errors, it is expected that you tell us where the error occurs.
Also when debugging problems like this, the first thing you should do is print the
shape
(and maybe thedtype
) of the suspected variables.This is an
IndexError
in python, which means that we're trying to access an index which isn't there in the tensor. Below is a very simple example to understand this error.with this array
arr
in place, if we now try to assign any value to some index, for example to the index0
as in the case belowThen, we will get an
IndexError
, as below:The reason is that we are trying to access an index (here at 0th position), which is not there (i.e. it doesn't exist because we have an array of size
0
).So, in essence, such an array is useless and cannot be used for storing anything. Thus, in your code, you've to follow the traceback and look for the place where you're creating an array/tensor of size
0
and fix that.Essentially it means you don't have the index you are trying to reference. For example:
will give me the error you are referring to, because I haven't told Pandas how long my dataframe is. Whereas if I do the exact same code but I DO assign an index length, I don't get an error:
Hope that answers your question!