Python Forum
IndexError: list index out of range bug?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range bug?
#1
Hello!

I have a code that gets a string cell which is a date and converts it to a certain date format. The thing is, I am out of index and have no idea why. This is the code:

import pandas

df = read_csv('XYZ.csv')
numberofrows = len(df.index)																		
rownumber = -1

for line in range(numberofrows):

	MonthsDict = dict(January = '01', February = '02', March = '03', April = '04', May = '05', June = '06', July = '07', August = '08', September = '09', October = '10', November = '11', December = '12')

	if str(df.at[rownumber + 1,"StateStreet_Date"]) != "None" or str(df.at[rownumber + 1,"StateStreet_Date"]) != "NaN":
		SSdatelist = str(df.at[rownumber + 1,"StateStreet_Date"]).split(' ')
		df.at[rownumber + 1,"StateStreet_Date"] = SSdatelist[2] + '.' + str(MonthsDict.get(SSdatelist[1])) + '.' + SSdatelist[0] + '.'
The string we get from the cell is: "09 August 2019"
The end modified string should be: "2019.08.09."

For this I get an error which is this: IndexError: list index out of range. I tried some information gathering regarding the list. I am adding those here and also what they returned. The weird thing is that a lot of times they return two lists, as if one of them overwrites the good one and that is why I get an error ( I shouldn't get two list for 1 variable):

df = read_csv('Data_Acquisition.csv')
	MonthsDict = dict(January = '01', February = '02', March = '03', April = '04', May = '05', June = '06', July = '07', August = '08', September = '09', October = '10', November = '11', December = '12')
	if str(df.at[rownumber + 1,"StateStreet_Date"]) != "None" or str(df.at[rownumber + 1,"StateStreet_Date"]) != "NaN":
		SSdatelist = str(df.at[rownumber + 1,"StateStreet_Date"]).split(' ')
        print(SSdatelist)
        print(len(SSdatelist))
		df.at[rownumber + 1,"StateStreet_Date"] = SSdatelist[2] + '.' + str(MonthsDict.get(SSdatelist[1])) + '.' + SSdatelist[0] + '.'
Output:
['09', 'August', "19"] 3 ["2019.08.09."] 1 IndexError: list index out of range
I even went as far to list the elements with indexes in loop. All seemed fine:

Output:
[[0, '09'], [1, 'August'], [2, '2019']]
I tried every combination and it seems that when I try to print SSdatelist[1] I get the error, which means that the list has 1 element (only the 0 index). I print SSdatelist[0] and get:

Output:
09 2019.08.09. Traceback (most recent call last): File "as.py", line 36, in <module> df.at[rownumber + 1,"StateStreet_Date"] = SSdatelist[2] + '.' + str(MonthsDict.get(str(SSdatelist[1]))) + '.' + SSdatelist[0] + '.' #makes the date the correct format IndexError: list index out of range
How do I have two things under 1 variable? How is this possible? the first one it prints is correct(09) but the second one should not even get printed because it does not exists yet (2019.08.09.) because it only gets created after that.
Reply
#2
you are not showing how you increment rownum, and I expect this is related to index being zero based
since 'line' is a range of numberofrows, it will span the dataframe, so why not use it as your index?
example (line 11):
if str(df.at[line,"StateStreet_Date"]) != "None" or str(df.at[line,"StateStreet_Date"]) != "NaN":
also, use a more meaningful name, instead of line, use rownum, or idx
Reply
#3
It's a fair question and I have no excuse for it, I wrote the code when I was tired and working late and did not realise that adding the +1-s was a waste of time and i only should have changed rownumber to 0. I have only started learning some months ago when I have some, but I do realise this is very much a "noob" mistake.

But why does the error keep happening? Sorry if I did not understand something.

EDIT: The full code is not here as it has many other things and would be too long, that is why I did not include the rownumber = rownumber + 1 part because it's a bit further away.
Reply
#4
I can't see where you are incrementing rownum, you haven't shown that code,
but as I previously stated, it most likely has to do with index starting at zero.
thus if you keep incrementing rownum, at some point it will be equal to length of frame + 1, thus returning an index error.
if you surround your code with:
try:
    ... your code here ...
except IndexError:
    print(f"Index error, rownum: {rownum}")
    raise
this will show what rownum is equal to when the index error occurs.
Also, you should always post your error tracebacks, complete and untouched as they almost certainly pinpoint the cause of error.
(Use error tags)
Reply
#5
You were absolutely right, it's kind of embarrassing to admit, but I wrote the rownumber = rownumber + 1 outside the loop and that is what caused the error. Thank you for the help!
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 1,947 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,154 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,955 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,843 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,277 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,408 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,499 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,101 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,759 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,234 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav

Forum Jump:

User Panel Messages

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