I want to add a new column in a df which adds a week to a previous date.
What I have :
start_date num_of_weeks
01/5/2019. 1
01/12/2019. 2
01/12/2019. 2
01/19/2019. 3.
01/19/2019. 3
01/19/2019. 3
For the num_of_weeks corresponding to each start_date I want to add one week ( 7 days) to that date.
So If num_of_weeks = 3 then
week1 = start_date 7
week2 = week1 7
week3 = week2 7
** Desired Output = **
start_date num_of_weeks weeks
01/5/2019. 1 01/12/2019
01/12/2019. 2 01/19/2019
01/12/2019. 2 01/26/2019
01/19/2019. 3. 01/26/2019
01/19/2019. 3. 02/04/2019
01/19/2019. 3 02/11/2019
CodePudding user response:
First, we'll group by "start_date"
and create some kind of "subindex" to assign a number to each row by using pd.Series.rank
. This new value will show us how many weeks to add to each row:
>> df["extra_weeks"] = df.groupby("start_date").rank("first", ascending=False)
>> df
start_date num_of_weeks extra_weeks
0 2019-01-05 1 1.0
1 2019-01-12 2 1.0
2 2019-01-12 2 2.0
3 2019-01-19 3 1.0
4 2019-01-19 3 2.0
5 2019-01-19 3 3.0
Now, you could use pd.to_timedelta
on the "extra_weeks"
column and add that result to your "start_date"
column:
>> df["weeks"] = df["start_date"] pd.to_timedelta(df["extra_weeks"], unit="w")
>> df
start_date num_of_weeks extra_weeks weeks
0 2019-01-05 1 1.0 2019-01-12
1 2019-01-12 2 1.0 2019-01-19
2 2019-01-12 2 2.0 2019-01-26
3 2019-01-19 3 1.0 2019-01-26
4 2019-01-19 3 2.0 2019-02-02
5 2019-01-19 3 3.0 2019-02-09
CodePudding user response:
This is a good opportunity to leverage the dateutil library. Specifically you'll be interested in the relativedelta function.