Python Forum
For loop index out of bounds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop index out of bounds
#1
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?
Yoriz write Feb-07-2022, 11:27 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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])
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,119 Sep-14-2023, 06:54 AM
Last Post: Mehboob
  Replace for loop to search index position illmattic 5 1,285 Sep-03-2022, 04:04 PM
Last Post: illmattic
  IndexError: index 10 is out of bounds for axis 0 with size 1 vahid89 1 12,274 Jan-07-2021, 06:19 PM
Last Post: deanhystad
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 5,026 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  String index out of bounds ( Python : Dict ) kommu 2 2,403 Jun-25-2020, 08:52 PM
Last Post: menator01
  IndexError: index 0 is out of bounds for axis 0 with size 0 error tmhsa 5 4,479 Apr-25-2020, 01:15 PM
Last Post: tmhsa
  IndexError: index 8 is out of bounds for axis 0 with size 8 Help_me_Please 6 65,915 Jan-03-2020, 01:51 PM
Last Post: Help_me_Please
  Get all values of for loop with an index BollerwagenIng 2 2,507 Aug-09-2019, 07:58 AM
Last Post: BollerwagenIng
  cannot import scipy.optimize.Bounds larkypython 2 7,247 May-05-2019, 04:09 AM
Last Post: larkypython
  Index error using pop in nested loop PerksPlus 3 3,419 Mar-28-2019, 03:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020