检查日期是否是 pandas 数据框中的营业日期 [英] Check if a date is a business date within a pandas dataframe

查看:62
本文介绍了检查日期是否是 pandas 数据框中的营业日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了python问题的工作日,但无法弄清楚如何解决以下问题.

I have already checked the business days in python question but wasn't able to figure out how to do the following problem.

我想检查日期是工作日还是周末还是假日,如果要添加日期,请添加下一个工作日之前的日期,并添加新列,如以下示例所示:

I want to check whether a date is a business date or weekend or holiday and if it is, I would like to add days until the next business day and append a new column as in the examples provided below:

我有以下熊猫数据框:

    date    
0    11/4/17  # Saturday
1    11/8/17    
2   11/16/17    
3   11/17/17    
4   11/19/17  # Sunday
5   11/22/17    
6   11/23/17    

然后,我想创建一个新列 x ['date_business'] ,其中包含所有营业日期,如以下示例所示:

And then I would like to create a new column x['date_business'] with all business dates as in the following example:

    date    date_business
0    11/4/17     11/6/17
1    11/8/17     11/8/17
2   11/16/17    11/16/17
3   11/17/17    11/17/17
4   11/19/17    11/20/17
5   11/22/17    11/22/17
6   11/23/17    11/23/17

推荐答案

我认为您需要 to_datetime + BDay

from pandas.tseries.offsets import *
df.date=pd.to_datetime(df.date)# convert your datetime into pandas
df['New']=np.where(df.date.dt.weekday_name.isin(['Saturday','Sunday']),df.date+BDay(1),df.date)

df
Out[1118]: 
        date        New
0 2017-11-04 2017-11-06
1 2017-11-08 2017-11-08
2 2017-11-16 2017-11-16
3 2017-11-17 2017-11-17
4 2017-11-19 2017-11-20
5 2017-11-22 2017-11-22
6 2017-11-23 2017-11-23

这篇关于检查日期是否是 pandas 数据框中的营业日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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