Python Forum
For loop index out of bounds - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: For loop index out of bounds (/thread-36326.html)



For loop index out of bounds - armitron121 - Feb-07-2022

Hello,

I am iterating a for loop a data frame by assigning the length of the df to a variable, and iterating the for loop across the range of that variable. This has worked for me, but I copied the code to replicate it but with different raw data / data frame and now I`m getting an error that the index is out of bounds.

#Create a new empty list for End Date - and populate the list using a if statement nested in a for loop
end_date=[]
length2=len(kl_50_132_transposed)
for i in range(length2): 
    if kl_50_132_transposed.iat[i,2] != kl_50_132_transposed.iat[i-1,2]:
        end_date.append(kl_50_132_transposed.iat[i+1,3])
    else:
        end_date.append("")
This returns the below error text

IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15288/1266775606.py in <module>
     92 for i in range(length2):
     93     if kl_50_132_transposed.iat[i,2] != kl_50_132_transposed.iat[i-1,2]:
---> 94         end_date.append(kl_50_132_transposed.iat[i+1,3])
     95     else:
     96         end_date.append("")

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   2220 
   2221         key = self._convert_key(key)
-> 2222         return self.obj._get_value(*key, takeable=self._takeable)
   2223 
   2224     def __setitem__(self, key, value):

~\anaconda3\lib\site-packages\pandas\core\frame.py in _get_value(self, index, col, takeable)
   3564         if takeable:
   3565             series = self._ixs(col, axis=1)
-> 3566             return series._values[index]
   3567 
   3568         series = self._get_item_cache(col)

IndexError: index 73 is out of bounds for axis 0 with size 73
I`ve confirmed that kl_50_132_transposed is a data frame with 73 rows. Any thoughts?


RE: For loop index out of bounds - deanhystad - Feb-08-2022

A length 73 array has index values 0..72. 73 is out of range.

It should always draw suspicion when you loop "i" over the length of an array and use "i+1" as an array index.
for i in range(length2): 
    if kl_50_132_transposed.iat[i,2] != kl_50_132_transposed.iat[i-1,2]:
        end_date.append(kl_50_132_transposed.iat[i+1,3])



RE: For loop index out of bounds - armitron121 - Feb-08-2022

I`ve used the same for loop + if statement design for other data frames that worked successfully. For example the below code does not give me the error. "kl_50_104" is a data frame with index 0 to 43, length2 is previously set to len(kl_50_104) which ends up being 44.
end_date=[]

for i in range(length2): 
    if kl_50_104_transposed.iat[i,2] != kl_50_104_transposed.iat[i-1,2]:
        end_date.append(kl_50_104_transposed.iat[i+1,3])
    else:
        end_date.append("")

I think I answered my question. In the case where I`m getting this error the last row of my dataframe is triggering the if statement which causes the append to look for the i+1 index, which doesnt exist. Looking at my other data frames where this worked, it looks like the last two rows of the data frame happened to equal each other in column 2, so the if statement was never trigged for the final index of the dataframe. I can probably solve this by adding a empty row to the data frame and iterating over the length minus 1.

Interested if there are better solutions out there.