Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"IndexError: positional indexers are out-of-bounds" when they're demonstrably not

Here's an MWE of some code I'm using. I slowly whittle down an initial dataframe via slicing and some conditions until I have only the rows that I need. Each block of five rows actually represents a different object so that, as I whittle things down, if any one row in each block of five meets the criteria, I want to keep it -- this is what the loop over keep.index accomplishes. No matter what, when I'm done I can see that the final indices I want exist, but I get an error message saying "IndexError: positional indexers are out-of-bounds." What is happening here?

import pandas as pd
import numpy as np

temp = np.random.rand(100,5)

df = pd.DataFrame(temp, columns=['First', 'Second', 'Third', 'Fourth', 'Fifth'])

df_cut = df.iloc[10:]

keep = df_cut.loc[(df_cut['First'] < 0.5) & (df_cut['Second'] <= 0.6)]

new_indices_to_use = []
for item in keep.index:
    remainder = (item % 5)
    add = np.arange(0-remainder,5-remainder,1)
    inds_to_use = item + add
    new_indices_to_use.append(inds_to_use)

new_indices_to_use = [ind for sublist in new_indices_to_use for ind in sublist]
final_indices_to_use = []
for item in new_indices_to_use:
    if item not in final_indices_to_use:
        final_indices_to_use.append(item)

final = df_cut.iloc[final_indices_to_use]
like image 666
Arnold Avatar asked May 22 '17 22:05

Arnold


People also ask

What does 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.

What is difference between LOC and ILOC?

The main difference between pandas loc[] vs iloc[] is loc gets DataFrame rows & columns by labels/names and iloc[] gets by integer Index/position. For loc[], if the label is not present it gives a key error. For iloc[], if the position is not present it gives an index error.


1 Answers

From Pandas documentation on .iloc (emphasis mine):

Pandas provides a suite of methods in order to get purely integer based indexing. The semantics follow closely python and numpy slicing. These are 0-based indexing.

You're trying to use it by label, which means you need .loc

From your example:

>>>print df_cut.iloc[89]
...
Name: 99, dtype: float64

>>>print df_cut.loc[89]
...
Name: 89, dtype: float64
like image 103
TemporalWolf Avatar answered Sep 16 '22 14:09

TemporalWolf