I have a df of holiday dates and I'm trying to make a list of dates six weeks before the holiday to add to the df. There are three lists PreList (the day before), DuringList (the day off) and PostList (the day after). I am using datetime.timedelta to get the six weeks before the holiday but I don't like all of the if statements. How can I reduce the if statements?
PreList = []
DuringList = []
PostList = []
for i in range(len(df)):
#for Mon
if df.date[i].weekday() == 0:
preDay = df.date[i] - datetime.timedelta(days = 3)
preWeek = preDay - datetime.timedelta(weeks = 6)
during = df.date[i]
postDay = df.date[i] + datetime.timedelta(days = 1)
postWeek = postDay - datetime.timedelta(weeks = 6)
PreList.append(preWeek)
DuringList.append(during)
PostList.append(postWeek)
#for Fri
elif df.date[i].weekday() == 4:
preDay = df.date[i] - datetime.timedelta(days = 1)
preWeek = preDay - datetime.timedelta(weeks = 6)
during = df.date[i]
postDay = df.date[i] + datetime.timedelta(days = 3)
postWeek = postDay - datetime.timedelta(weeks = 6)
PreList.append(preWeek)
DuringList.append(during)
PostList.append(postWeek)
#for Sat
elif df.date[i].weekday() == 5:
pre = df.date[i] - datetime.timedelta(days = 1)
preWeek = preDay - datetime.timedelta(weeks = 6)
during = df.date[i]
postDay = df.date[i] + datetime.timedelta(days = 2)
postWeek = postDay - datetime.timedelta(weeks = 6)
PreList.append(preWeek)
DuringList.append(during)
PostList.append(postWeek)
#for Sun
elif df.date[i].weekday() == 6:
preDay = df.date[i] - datetime.timedelta(days = 2)
preWeek = preDay - datetime.timedelta(weeks = 6)
during = df.date[i]
postDay = df.date[i] + datetime.timedelta(days = 1)
postWeek = postDay + datetime.timedelta(weeks = 6)
PreList.append(preWeek)
DuringList.append(during)
PostList.append(postWeek)
#for Tue - Thu
else:
preDay = df.date[i] - datetime.timedelta(days = 1)
preWeek = preDay - datetime.timedelta(weeks = 6)
during = df.date[i]
postDay = df.date[i] + datetime.timedelta(days = 1)
postWeek = postDay + datetime.timedelta(weeks = 6)
PreList.append(preWeek)
DuringList.append(during)
PostList.append(postWeek)