Dates rates
7/26/2019 1.04
7/30/2019 1.0116
7/31/2019 1.005
8/1/2019 1.035
8/2/2019 1.01
8/6/2019 0.9886
8/7/2019 1.0048
8/8/2019 0.97
8/9/2019 0.9659
8/12/2019 0.965
In the above dataframe, 29th July 2019 and 5th August 2019 are missing. These are weekdays. I need to populate the missing weekdays dates and assign the 'rate' which is next to missing date. For example: Assign the 30th july 2019 'rate' to the missing 29th july 2019 as well. Please suggest. Thanks
You can use for example pd.offsets.BDay()
to get next business day.
Initial dataframe (the Dates
column is of DateTime type):
Dates rates
0 2019-07-26 1.0400
1 2019-07-30 1.0116
2 2019-07-31 1.0050
3 2019-08-01 1.0350
4 2019-08-02 1.0100
5 2019-08-06 0.9886
6 2019-08-07 1.0048
7 2019-08-08 0.9700
8 2019-08-09 0.9659
9 2019-08-12 0.9650
Then:
df = df.merge(pd.DataFrame({'Dates':df['Dates'] + pd.offsets.BDay()}), on='Dates', how='outer').sort_values('Dates').bfill().dropna().reset_index(drop=True)print(df)
Prints:
Dates rates
0 2019-07-26 1.0400
1 2019-07-29 1.0116
2 2019-07-30 1.0116
3 2019-07-31 1.0050
4 2019-08-01 1.0350
5 2019-08-02 1.0100
6 2019-08-05 0.9886
7 2019-08-06 0.9886
8 2019-08-07 1.0048
9 2019-08-08 0.9700
10 2019-08-09 0.9659
11 2019-08-12 0.9650