I have pandas dataframe with a non-continuous date index (missing are weekends and holidays). I want to add column which would contain number of days until next day off.
Here is code generating example dataframe with desired values in till_day_off column:
import pandas as pd
df = pd.DataFrame(index=pd.date_range(start="2022-06-06", periods=15))
df["day_of_week"] = df.index.dayofweek # adding column with number of day in a week
df = df[(df.day_of_week < 5)] # remove weekends
df = df.drop(index="2022-06-15") # remove Wednesday in second week
df["till_day_off"] = [5,4,3,2,1,2,1,2,1,1] # desired values, end of column is treated as day off
Resulting dataframe:
day_of_week | till_day_off | |
---|---|---|
2022-06-06 | 0 | 5 |
2022-06-07 | 1 | 4 |
2022-06-08 | 2 | 3 |
2022-06-09 | 3 | 2 |
2022-06-10 | 4 | 1 |
2022-06-13 | 0 | 2 |
2022-06-14 | 1 | 1 |
2022-06-16 | 3 | 2 |
2022-06-17 | 4 | 1 |
2022-06-20 | 0 | 1 |
Real dataframe has over 7_000 rows so obviously I am trying to avoid iteration over rows. Any idea how to tackle the issue?