Python Pandas : Return the consecutive missing weekdays dates and assign rate next to missing dates in a dataframe

后端 未结 2 884
南方客
南方客 2021-01-16 08:53
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/12/2019   0.965

df = df.merge(
    p         


        
2条回答
  •  执笔经年
    2021-01-16 09:48

    You could create a Series of all business days then outer merge and bfill the missing values. This will retain any non-business days in your initial DataFrame (if any) and will also use their values in the filling.

    import pandas as pd
    #df['Dates'] = pd.to_datetime(df['Dates'])
    
    s = pd.Series(pd.date_range(df['Dates'].min(), df['Dates'].max(), freq='D'),
                  name='Dates')
    s = s[s.dt.dayofweek.lt(5)]
    
    df = df.merge(s, how='outer').sort_values('Dates').bfill()
    

            Dates   rates
    0  2019-07-26  1.0400
    7  2019-07-29  1.0116
    1  2019-07-30  1.0116
    2  2019-07-31  1.0050
    3  2019-08-01  1.0350
    4  2019-08-02  1.0100
    8  2019-08-05  0.9886
    5  2019-08-06  0.9886
    9  2019-08-07  0.9650
    10 2019-08-08  0.9650
    11 2019-08-09  0.9650
    6  2019-08-12  0.9650
    

提交回复
热议问题