Python’s data types are one of the language’s best features. The language has some powerful options by default. And third-party libraries like Pandas and NumPy add even more options for complex features like multidimensional data structures. However, merging Python’s syntax and these new features isn’t always a totally straightforward experience. Many people working with advanced data types encounter the “indexerror: single positional indexer is out-of-bounds” error message. But it’s a relatively straightforward issue to both understand and fix.
The Central Issue Causing This Index Error
As the error’s name suggests, this is usually a problem that springs up when trying to access a non-existent index within a container. This is particularly common when trying to access information outside a multi-dimensional data structure like a Pandas dataframe.
A Closer Look at the Index Error
Python’s flexibility in data types is certainly impressive. And it’s one of the points that tend to attract people to the platform. However, the wealth of options can lead to some confusion when we make assumptions about the functionality of various data types. And this is especially true for data types from Pandas and NumPy. The more advanced options from these frameworks open up new ways to work with data structures. But at the same time, it also means that we need to change how we work with these items.
It’s important to note that these new data types, such as a Pandas dataframe, have their own unique properties. A Pandas dataframe, a dictionary type, and a list might all seem similar at first glance. But we use different syntax to work with each of them. And in the case of dataframes, this also means taking a multi-dimensional structure into account.
Incorrectly handling a dataframe’s structure is a common cause of the “indexerror: single positional indexer is out-of-bounds” error message. This can occur simply due to mistaken assumptions about the behavior of different collections when compared with each other. It can also occur simply due to a mistake in Python’s method of iteration. The language begins indexing with 0. So, for example, referencing position 1 in a list would actually refer to the 2nd item in it. That is, unless we’re working with a multi-dimensional container. In which case we’d need to first reference which position we’re looking at within it.
However, this will usually bring up a specific Python error with details about the index axis and size. It’s still an index error, but one with more precise information than we’re seeing in the “indexerror: single positional indexer is out-of-bounds” error. But we will see a less concise error if Python’s interpreter is itself unsure about what we’re trying to access. For example, slice indexers can complicate this issue and raise the error without more explanatory details.
How To Fix the Index Error
Actually fixing this problem is fairly straightforward. But we’ll need to begin by first triggering the error. Consider the following Python code.
import pandas as pd
df = pd.DataFrame({‘State’: [‘Alabama’, ‘Alaska’, ‘Arizona’, ‘Arkansas’, ‘California’],’City’: [‘Montgomery’, ‘Juneau’, ‘Phoenix’, ‘Little Rock’, ‘Sacramento’], ‘Bird’: [‘Yellowhammer’, ‘Willow ptarmigan’, ‘Cactus wren’, ‘Northern mockingbird’, ‘California quail’]})
print(df.iloc[:, 3])
We begin by importing the Pandas library using the standard assignment alias of pd. Next, we create a variable called df to store a new Pandas dataframe. We’ll populate it with a list of American states along with their capitals and emblematic birds.
Now, imagine that we want to print out a list of the state birds. In this case, we access it with the iloc to slice from what we assume is the bird-related data. We access the Pandas iloc to use an integer-location-based indexing system.
But instead of printing the birds to screen, we’ll instead see the “indexerror: single positional indexer is out-of-bounds” error. This is because the syntax should use “value-1” rather than “value” for the position. We’re essentially trying to access a non-existent fourth category that would follow after the one we’re really looking for. This is further illustrated if we use more precise information with iloc. Try running the following code.
import pandas as pd
df = pd.DataFrame({‘State’: [‘Alabama’, ‘Alaska’, ‘Arizona’, ‘Arkansas’, ‘California’],’City’: [‘Montgomery’, ‘Juneau’, ‘Phoenix’, ‘Little Rock’, ‘Sacramento’], ‘Bird’: [‘Yellowhammer’, ‘Willow ptarmigan’, ‘Cactus wren’, ‘Northern mockingbird’, ‘California quail’]})
print(df.iloc[3, 2])
We now see “Northern mockingbird” printed to screen. This highlights the fact that we need to use “value-1” when finding positions. We can now try to get the full slice using the corrected syntax.
import pandas as pd
df = pd.DataFrame({‘State’: [‘Alabama’, ‘Alaska’, ‘Arizona’, ‘Arkansas’, ‘California’],’City’: [‘Montgomery’, ‘Juneau’, ‘Phoenix’, ‘Little Rock’, ‘Sacramento’], ‘Bird’: [‘Yellowhammer’, ‘Willow ptarmigan’, ‘Cactus wren’, ‘Northern mockingbird’, ‘California quail’]})
print(df.iloc[:, 2])
The Python interpreter will run through the code cleanly and without any error messages. And we now see the full set of birds from the Pandas dataframe printed to screen.