Register Login

indexerror: Single positional indexer is out-of-bounds Error

Updated Jul 20, 2021

Indexing in large and complex data sets plays a critical role in storing and handling data. When we deal with compound data types like lists and tuples or data sets having rows and columns in data science, we frequently use index values within square brackets to use them. In this article, we will talk about the index-based error: single positional indexer is out-of-bounds.

What is this “Indexerror: single positional indexer is out-of-bounds” error?

This is an index-based error that pops up when programmers try to access or call or use any memory that is beyond the scope of the index. Let suppose, you have a list that has five elements. This means, your index will start from 0 up till 4. But now, if you try to access or display or change the value of the 7th index, will it be possible? No, because your index range lies within 0 and 4. This is what we called bound. But, accessing elements exceeding the bound is what the Python interpreter calls an out-of-bounds situation.

indexerror: Single positional indexer is out-of-bounds Error

Indexerror in case of dataset accessing:

Let suppose, you have a dataset Y = Dataset.iloc[:,18].values

In this case, if you are experiencing “Indexing is out of bounds” error, then most probably this is because there are less than 18 columns in your dataset, and you are trying to access something that does not exists. So, column 18 or less does not exist.

Indexerror in case of unknown DataFrame Size:

Such an error also occurs when you have to index a row or a column having a number greater than the dimensions of your DataFrame. For example, if you try to fetch the 7th column from your DataFrame when you have only three columns defined like this.

Error Code:

import pandas as pd

df = pd.DataFrame({'Name': ['Karl', 'Ray', 'Gaurav', 'Dee', 'Sue'],

                   'City': ['London', 'Montreal', 'Delhi', 'New York', 'Glasgow'],

                   'Car': ['Maruti', 'Audi', 'Ferrari', 'Rolls Royce', ' Tesla'] })

print(df)

x = df.iloc[0, 8]

print(x)

Output:

raise IndexError("single positional indexer is out-of-bounds")

IndexError: single positional indexer is out-of-bounds

This program creates an error because the second size attribute () we want to fetch does not exist.

This also happens if the programmer misunderstood the iloc() function. The iloc() is used to select a particular cell of the dataset or data in a tabular format. Any data that belongs to a particular row or column from a set of values within a dataframe or dataset.

In this function, the value before the comma(,) defines the index of rows & the after ‘,’ represents the index of columns. But if your data does not lie within the range, then iloc() won’t be able to fetch any data and hence will show this error.

Correct code:

import pandas as pd

df = pd.DataFrame({'Name': ['Karl', 'Ray', 'Gaurav', 'Dee', 'Sue'],

                   'City': ['London', 'Montreal', 'Delhi', 'New York', 'Glasgow'],

                   'Car': ['Maruti', 'Audi', 'Ferrari', 'Rolls Royce', ' Tesla'] })

print(df)

x = df.iloc[3, 0]

print("\n Fetched value using the iloc() is: ", x)

Output:

     Name      City          Car

0    Karl    London       Maruti

1     Ray  Montreal         Audi

2  Gaurav     Delhi      Ferrari

3     Dee  New York  Rolls Royce

4     Sue   Glasgow        Tesla

Fetched value using the iloc() is:  Dee

Explanation:

First we create the DataFrame (2-D dataset) with three columns and five rows and print it. Here we have mentioned the exact row and column value for which we are not receiving any error. Therefore, to resolve such “indexerror single positional indexer is out-of-bounds” error, we have to first check the outer bound of the rows and columns existing in our dataset.

Conclusion:

To eliminate such error messages and not to encounter such errors repeatedly, programmers need to focus on the retrieval of particular count of row and columns. Also, programmers should focus on checking the valid range of index values. Also it is easy and comfortable to use "iloc()" for retrieving any value a programmer wants. But the programmer needs to make sure that they refer to the correct index values, otherwise, “Indexerror: single positional indexer is out-of-bounds” error will pop up.


×