Reputation: 175
I have a dataframe that contains a data column
Comp_date
0 2020-04-24
1 NaT
2 NaT
3 NaT
4 2020-08-06
5 NaT
6 NaT
7 NaT
8 2020-08-22
9 NaT
I am trying to fill the null with the value of the previous date + add a constant number of days (10). But I am unable to do so. I tried the following
df['Comp_date']=df['Comp_date'].fillna((df['Comp_date'].shift()+pd.to_timedelta(10, unit='D')), inplace=True)
Nothing happens and I get the same result. Any help? expected outcome
Comp_date
0 2020-04-24
1 2020-05-04
2 2020-05-14
3 2020-05-24
4 2020-08-06
5 2020-08-16
6 2020-08-26
7 2020-09-05
8 2020-08-22
9 2020-09-01
Upvotes: 1
Views: 857
Reputation: 862511
Idea is create groups for missing values by Series.notna
and Series.cumsum
and create counter by GroupBy.cumcount
, multiple number of days by Series.mul
convert to timedeltas by to_timedelta
what is added to forward filling missing values with ffill
:
num_days = 10
g = df['Comp_date'].notna().cumsum()
days = pd.to_timedelta(df.groupby(g).cumcount().mul(num_days), unit='d')
df['Comp_date'] = df['Comp_date'].ffill().add(days)
print (df)
Comp_date
0 2020-04-24
1 2020-05-04
2 2020-05-14
3 2020-05-24
4 2020-08-06
5 2020-08-16
6 2020-08-26
7 2020-09-05
8 2020-08-22
9 2020-09-01
Upvotes: 2
Reputation: 109528
I'm not clear on your question, but this adds a constant number of days to the last observed Comp_date
.
constant_number_of_days = 2
df2 = df['Comp_date'].ffill().to_frame()
df2.loc[df['Comp_date'].isnull(), 'Comp_date'] += pd.Timedelta(days=constant_number_of_days)
>>> df2
Comp_date
0 2020-04-24
1 2020-04-26
2 2020-04-26
3 2020-04-26
4 2020-08-06
5 2020-08-08
6 2020-08-08
7 2020-08-08
8 2020-08-22
9 2020-08-24
Upvotes: 2