Let's say I have the starting input table below.
| Name | _leavestartdate | _leaveenddate |
|---|---|---|
| Jerry Rice | 2023-12-26 | 2023-12-26 |
| Pablo Picasso | 2023-08-15 | 2023-08-21 |
| Ray Ramon | 2023-03-16 | 2023-03-17 |
The Python code to create it is:
import pandas as pd
df = pd.DataFrame({'name':["Jerry Rice","Pablo Picasso","Ray Ramon"],'_leavestartdate':["2023-12-26","2023-08-15","2023-03-16"],'_leaveenddate':["2023-12-26","2023-08-21","2023-03-17"]})
First, I want to get all dates between the _leavestartdate and _leaveenddate split based on Name column. The list of dates would populate under new column "Date". I also want to exclude any weekend dates in the newly reformatted table.
So my desired output table is shown below. Note that it only shows weekday dates and excludes the 2 dates (8/19/2023 and 8/20/2023) from Pablo Picasso that are weekend dates.
| Name | Date | _leavestartdate | _leaveenddate |
|---|---|---|---|
| Jerry Rice | 2023-12-26 | 2023-12-26 | 2023-12-26 |
| Pablo Picasso | 2023-08-15 | 2023-08-15 | 2023-08-21 |
| Pablo Picasso | 2023-08-16 | 2023-08-15 | 2023-08-21 |
| Pablo Picasso | 2023-08-17 | 2023-08-15 | 2023-08-21 |
| Pablo Picasso | 2023-08-18 | 2023-08-15 | 2023-08-21 |
| Pablo Picasso | 2023-08-21 | 2023-08-15 | 2023-08-21 |
| Ray Ramon | 2023-03-16 | 2023-03-16 | 2023-03-17 |
| Ray Ramon | 2023-03-17 | 2023-03-16 | 2023-03-17 |
I looked at previous solutions in StackOverflow and tried the following code:
date = pd.DataFrame(pd.date_range(start=df.min()._leavestartdate,
end=df.max()._leaveenddate), columns=['Date'])
df_final = pd.merge(left=date, right=df, left_on='Date', right_on='_leavestartdate',
how='outer').fillna(method='ffill')
But the solution produces the following wrong table because it's not accounting for the "Name" column:
How can I fix the code to get my desired output table?

cross joinbetweendateanddfthen filter to return only rows whereDateis>=_leavestartdateand<=_leaveendddate. Not sure the exact syntax off the top of my head and it's not very efficient in the tables are large.isin()to check if it's in Saturday/Sunday).