# What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?

Table of Contents

## What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?

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]: x
Out[436]: array([], dtype=int32)
In [437]: x[0]
...
IndexError: index 0 is out of bounds for axis 0 with size 0
```

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 the `dtype`

) of the suspected variables.

## Applied to `pandas`

- The 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`

block - Verify the size of the array is not 0
`if x.size != 0:`

## What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?

Essentially it means you don’t have the index you are trying to reference. For example:

```
df = pd.DataFrame()
df['this']=np.nan
df['my']=np.nan
df['data']=np.nan
df['data'][0]=5 #I haven't yet assigned how long df[data] should be!
print(df)
```

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:

```
df = pd.DataFrame(index=[0,1,2,3,4])
df['this']=np.nan
df['is']=np.nan
df['my']=np.nan
df['data']=np.nan
df['data'][0]=5 #since I've properly labelled my index, I don't run into this problem!
print(df)
```

Hope that answers your question!

## What does ‘index 0 is out of bounds for axis 0 with size 0’ mean?

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.

```
# 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) <ipython-input-16-0891244a3c59> in <module> ----> 1 arr[0] = 23 IndexError: index 0 is out of bounds for axis 0 with size 0`

The reason is that we are trying to access an index (here at 0^{th} 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]: 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.