Similar to your other question:
First convert to datetimes:
df.loc[:, ["Start", "End"]] = (df.loc[:, ["Start", "End"]]
.transform(pd.to_datetime, format="%m/%d/%Y"))
df
identity Start End week
0 E 2020-06-18 2020-07-02 1
1 E 2020-06-18 2020-07-02 2
2 2D 2020-07-18 2020-08-01 1
3 2D 2020-07-18 2020-08-01 2
4 A1 2020-09-06 2020-09-20 1
5 A1 2020-09-06 2020-09-20 2
Your identity is in groups of two, so I'll use that when selecting dates from the date_range:
from itertools import chain
result = df.drop_duplicates(subset="identity")
date_range = (
pd.date_range(start, end, freq="7D")[:2]
for start, end in zip(result.Start, result.End)
)
date_range = chain.from_iterable(date_range)
End = lambda df: df.Start.add(pd.Timedelta("7 days"))
Create new dataframe:
df.assign(Start=list(date_range), End=End)
identity Start End week
0 E 2020-06-18 2020-06-25 1
1 E 2020-06-25 2020-07-02 2
2 2D 2020-07-18 2020-07-25 1
3 2D 2020-07-25 2020-08-01 2
4 A1 2020-09-06 2020-09-13 1
5 A1 2020-09-13 2020-09-20 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…