I want to loop through a date range that has only business days in it, i.e. no weekends. To do that, I have the following command that basically picks every third Friday in a month and makes the column window equal to 2, 20 days from this third Friday. This code works totally fine.
for beg in pd.bdate_range("2000-01-01", "2017-05-01"):
beg= third_friday
df["window"].loc[beg: beg + pd.to_timedelta(20,"D")] = 2
if month==12:
year=year+1
month=0
if year>=2017 and month>=3:
break
month = month +3
monthcal = c.monthdatescalendar(year,month)
third_friday = [day for week in monthcal for day in week if \
day.weekday() == calendar.FRIDAY and \
day.month == month][2]
However, the 20 in the
df["window"].loc[beg: beg + pd.to_timedelta(20,"D")] = 2
command refers to 20 days INCLUDING weekends, but I want it to refer to 20 WEEKDAYS; e.g. something like this:
df["window"].loc[beg: beg + pd.to_timedelta(20, "Weekdays_only")] = 2
Is there an easy fix so that I can replace the "D" with something else or do I have to rewrite everything?
Moreover, I also want to mark the days around the third Fridays with different values, e.g. day +1 after third_friday is 1 and day+2 is 2. To do that, I wrote a second for loop. Here the full example:
for beg in pd.bdate_range("2000-01-01", "2017-05-01"):
beg= third_friday
lower_counter = 0
for j in range(0,-21,-1):
df["window_counter"].loc[beg - pd.to_timedelta(j,"D"):beg] = lower_counter
lower_counter = j
df["window"].loc[beg: beg + pd.to_timedelta(20,"D")] = 2
if month==12:
year=year+1
month=0
if year>=2017 and month>=3:
break
month = month +3
monthcal = c.monthdatescalendar(year,month)
third_friday = [day for week in monthcal for day in week if \
day.weekday() == calendar.FRIDAY and \
day.month == month][2]