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.0column2 = data<1,1:>x_column1 = np.zeros(x.size+2)x_column1<1:-1> = xx_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),(column1
python numpy indexing error-handling index-error
Improve this question
edited Feb 8 "19 at 22:17
11k77 gold badges6060 silver badges7373 bronze badges
asked Jan 5 "17 at 18:37
Seoyeon HongSeoyeon Hong
35322 gold badges44 silver badges88 bronze badges
Add a comment |
3 Answers 3
Active Oldest Votes
In numpy, index and dimension numbering starts with 0. So axis 0 means the 1st dimension. Also in numpy a dimension can have length (size) 0. The simplest case is:
In <435>: x = np.zeros((0,), int)In <436>: xOut<436>: array(<>, dtype=int32)In <437>: x<0>...IndexError: index 0 is out of bounds for axis 0 with size 0I also get it if x = np.zeros((0,5), int), a 2d array with 0 rows, and 5 columns.
You are watching: Indexerror: index 0 is out of bounds for axis 0 with size 0
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 the dtype) of the suspected variables.
Applied to pandasThe same error can occur for those using pandas, when sending a Series or DataFrame to a numpy.array, as with the following:
Resolving the error:Use a try-except blockVerify the size of the array is not 0if x.size != 0:
Improve this answer
edited Jan 12 at 0:41
36.5k2121 gold badges7575 silver badges9393 bronze badges
answered Jan 5 "17 at 19:02
187k1313 gold badges187187 silver badges298298 bronze badges
Add a comment |
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.
See more: Execution Failed Due To Configuration Error: Invalid Permissions On Lambda Function
# create an empty array of dimension `0`In <14>: arr = np.array(<>, dtype=np.int64) # check its shape In <15>: arr.shape Out<15>: (0,)with this array arr in place, if we now try to assign any value to some index, for example to the index 0 as in the case below
In <16>: arr<0> = 23 Then, we will get an IndexError, as below:
IndexError Traceback (most recent call last) in ----> 1 arr<0> = 23IndexError: index 0 is out of bounds for axis 0 with size 0The 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).
In <19>: arr.size * arr.itemsize Out<19>: 0So, 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.