Oct-18-2019, 06:26 AM
(This post was last modified: Oct-18-2019, 06:28 AM by CaptainCsaba.)
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:
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):
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.