Python Pandas:返回连续缺失的工作日日期,并在数据框中缺失日期旁边分配速率 [英] Python Pandas : Return the consecutive missing weekdays dates and assign rate next to missing dates in a dataframe

查看:202
本文介绍了Python Pandas:返回连续缺失的工作日日期,并在数据框中缺失日期旁边分配速率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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(
    pd.DataFrame({'Dates':df['Dates'] + pd.offsets.BDay()}), on='Dates', how='outer'
).sort_values('Dates').bfill().dropna().reset_index(drop=True)

print(df)

我尝试了上面的代码,但无法修复连续丢失的工作日.它只能修复1天.在上述数据框中,缺少了2019年7月29日以及8月5日,7月8日,8月​​9日.这些是工作日.我需要填充缺少的工作日日期,并分配缺少日期旁边的费率".例如:将2019年7月30日的'费率'分配给缺失的2019年7月29日,依此类推,对于所有缺失的日期.请提出建议.谢谢,我期待以下输出

I tried the above code but its unable to fix the consecutive missing business days. It can fix only for 1 day. In the above dataframe, 29th July 2019 then 5th, 7th , 8th , 9th August 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 and so on for all missing dates. Please suggest. Thanks I expect the following output

Dates       rates
7/26/2019   1.04
7/29/2019   1.0116
7/30/2019   1.0116
7/31/2019   1.005
8/1/2019    1.035
8/2/2019    1.01
8/5/2019    0.9886
8/6/2019    0.9886
8/7/2019    0.965
8/8/2019    0.965
8/9/2019    0.965
8/12/2019   0.965

推荐答案

,您可以使用 bdate_range ,仅在工作日的费率中创建所有缺少的值:

you can use reindex with bdate_range to create all the missing values in rates for business days only:

new_df = df.set_index('Dates')\
           .reindex( pd.bdate_range(df.Dates.min(), df.Dates.max(), name='Dates'), 
                     method='bfill')\
           .reset_index() 
print (new_df)
        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  0.9650
9  2019-08-08  0.9650
10 2019-08-09  0.9650
11 2019-08-12  0.9650

这篇关于Python Pandas:返回连续缺失的工作日日期,并在数据框中缺失日期旁边分配速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆