So I have a dataframe like this: df =
index | Date | Value |
---|---|---|
1 | 12/1/2022 | 100 |
2 | 12/2/2022 | 200 |
3 | 12/5/2022 | 500 |
4 | 12/6/2022 | 150 |
I'm trying to create a variable that is the sum of values where the date is within 7 business days of a given date (12/12/2022 in this example).
I was able to do 7 days (not only business days) using this function:
var = df.loc[(df['Date'] - date(2022,12,12)).dt.days.abs() < 7, 'Value'].sum()
Now I have a function that finds the number of business days between two dates:
def date_diff(date_1, date_2):
dates = []
if date_2 > date_1:
delta = (date_2 - date_1).days
start_date = date_1
else:
delta = (date_1 - date_2).days
start_date = date_2
for i in range(delta + 1):
date_ = start_date + timedelta(i)
if check(date_): # checks if its a business day
dates.append(date_)
return len(dates)
So this function won't take a Pandas Series and I don't know what to change to make it work. I fiddled with different syntaxes I've used like passing the Series with .dt or .values but nothing has worked. I'm assuming there is an easy solution I'm just overlooking, so any help would be greatly appreciated.