I understand that "list index out of range" means that I am trying to reference something that is outside of my list. However, I just can't seem to identify where I am doing this and how to fix it.
In my code I use a list of numbers from 1 to 39 (All Time) that I call "myranges". The numbers in this list represent a month and year starting in Nov 2014 and ending in Jan 2018. I then have other lists that help with my naming conventions that I zip with the "myranges" list.
This code has worked fine in the past when I run it for All Time, but now I only want to run it from Sept 2017 to Jan 2018 (35 to 39) and it gives me the Index Error.
Here is the full error:
cumulativepoints = "CumulativePatientVisits{0}_{1}_{2}".format(row[0], row[2], month_descr[myrange-1][1])
IndexError: list index out of range
Obviously, the error is coming from my naming convention, I assume the "month_desc" which is my zip list. However, whenever I print the "month_desc" it look correct to me.
Here is the month_desc printed out :
[(35, 'Sep2017', 'September', 9, 'Sep', 2017), (36, 'Oct2017', 'October', 10, 'Oct', 2017), (37, 'Nov2017', 'November', 11, 'Nov', 2017), (38, 'Dec2017', 'December', 12, 'Dec', 2017), (39, 'Jan2018', 'January', 1, 'Jan', 2018)]
Here is my full code. Could someone help me see what I'm doing wrong.
import arcpy
import datetime
from datetime import timedelta
import time
#Document Start Time in-order to calculate Run Time
time1 = time.clock()
#now = datetime.datetime.now()
arcpy.env.overwriteOutput = True
#Set the workspace
from arcpy import env
env.workspace = r"C:\arcGIS_Shared\Python\CenterHeatMaps10.gdb"
#Declare variables
fc = 'Open_GoHealth_Centers'
fields = ['USER_market_id','USER_GoHealth_ID','USER_GoHealth_Center_Name', 'USER_Opening_Date', 'USER_MonthsSinceFHOpening']
fieldname = 'USER_market_id'
#Define SQL clause statement
sqlclause = (None, 'Order By USER_market_id, USER_GoHealth_ID')
# loop through market
markets = [1000]
#lists that allow for A SPECIFIC month/year to be selected and named
myranges = [35,36,37,38,39]
monthyear = ['Sep2017','Oct2017','Nov2017','Dec2017','Jan2018','Feb2018','Mar2018','Apr2018','May2018','Jun2018','Jul2018','Aug2018','Sep2018','Oct2018','Nov2018','Dec2018','Jan2019','Feb2019','Mar2019','Apr2019','May2019','Jun2019','Jul2019','Aug2019','Sep2019','Oct2019','Nov2019','Dec2019','Jan2020','Feb2020','Mar2020','Apr2020','May2020','Jun2020','Jul2020','Aug2020','Sep2020','Oct2020','Nov2020','Dec2020','Jan2021','Feb2021','Mar2021','Apr2021','May2021','Jun2021','Jul2021','Aug2021','Sep2021','Oct2021','Nov2021','Dec2021','Jan2022','Feb2022','Mar2022','Apr2022','May2022','Jun2022','Jul2022','Aug2022','Sep2022','Oct2022','Nov2022','Dec2022','Jan2023','Feb2023','Mar2023','Apr2023','May2023','Jun2023','Jul2023','Aug2023','Sep2023','Oct2023','Nov2023','Dec2023','Jan2024','Feb2024','Mar2024','Apr2024','May2024','Jun2024','Jul2024','Aug2024','Sep2024','Oct2024','Nov2024','Dec2024','Jan2025','Feb2025','Mar2025','Apr2025','May2025','Jun2025','Jul2025','Aug2025','Sep2025','Oct2025','Nov2025','Dec2025']
monthname = ['September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',]
monthnum = [9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12]
monthabbr = ['Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
year = [2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018,2019,2019,2019,2019,2019,2019,2019,2019,2019, 2019, 2019, 2019,2020,2020,2020,2020,2020,2020,2020,2020, 2020, 2020, 2020,2020,2021,2021,2021,2021,2021,2021,2021, 2021, 2021, 2021, 2021, 2021,2022,2022,2022,2022,2022,2022,2022,2022, 2022, 2022, 2022, 2022,2023,2023,2023,2023,2023,2023,2023,2023, 2023, 2023, 2023, 2023,2024,2024,2024,2024,2024,2024,2024,2024, 2024, 2024, 2024, 2024,2025,2025,2025,2025,2025,2025,2025,2025, 2025, 2025, 2025, 2025]
month_description = zip(myranges,monthyear,monthname,monthnum,monthabbr,year)
month_descr = list(month_description)
print (month_descr)
#returns last day of each month
def last_day_of_month(any_day):
next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
return next_month - datetime.timedelta(days=next_month.day)
for market in markets:
print (market)
#Define WHERE clause statement
whereclause = """{} = """.format(arcpy.AddFieldDelimiters(fc, fieldname)) + str(market)
for myrange in myranges:
print(myrange)
#Cursor to create list of centers to loop through
with arcpy.da.SearchCursor(in_table = fc, where_clause = whereclause, field_names = fields, sql_clause = sqlclause) as cursor:
#Loop to run SummarizeWithin by center
for row in (cursor):
Set variables for SummarizeWithin
#Specify type of summarization, either Blocks or ZIPS, as part of the polys argument
polys = "Market{0}".format(row[0])+"Blocks"
cumulativepoints = "CumulativePatientVisits{0}_{1}_{2}".format(row[0], row[2], month_descr[myrange-1][1])
cumulativeoutFeatureClass = "CumulativeSumWithin{0}_{1}_{2}".format(row[0], row[2], month_descr[myrange-1][1])
keepAll = 'ONLY_INTERSECTING'
sumFields = [['USER_VisitCount', 'Sum']]
addShapeSum = 'ADD_SHAPE_SUM'
# This If statement determines if the Opening_Date of the respective center is before the last day of the month being run.
# If it is, the summarize-within is performed, if not, the feature class is skipped.
year = month_descr[myrange-1][5]
month = month_descr[myrange-1][3]
if last_day_of_month(datetime.date(year, month, 1)) >= row[3].date():
arcpy.SummarizeWithin_analysis(in_polygons = polys, in_sum_features = cumulativepoints, out_feature_class = cumulativeoutFeatureClass, keep_all_polygons = keepAll, sum_fields = sumFields, sum_shape = addShapeSum)
#Print Results
print(row[2])
print(myrange)
print (arcpy.GetMessages())
#Document End Time
time2 = time.clock()
#Run time in second
runtime = (time2-time1)
print (str(timedelta(seconds=runtime)))
The problem has arisen due to cutting down the code to only work for a certain period.
Your problem reduces down to the following:
myranges = [35,36,37,38,39]
month_descr = [(35, 'Sep2017', 'September', 9, 'Sep', 2017), (36, 'Oct2017', 'October', 10, 'Oct', 2017),
(37, 'Nov2017', 'November', 11, 'Nov', 2017), (38, 'Dec2017', 'December', 12, 'Dec', 2017),
(39, 'Jan2018', 'January', 1, 'Jan', 2018)]
for myrange in myranges:
month_descr[myrange-1][1]
Here you are looping through myranges
. The first value is 35 which you use as an index in the list month_descr
which has only 5 elements.
Your full code (1 to 39) works because your list would have 39 elements in and would produce no index error