确定一天是否是Python / Pandas中的工作日 [英] Determine if a day is a business day in Python / Pandas

查看:587
本文介绍了确定一天是否是Python / Pandas中的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个程序设置可以运行两种不同的方式。一种方法是在指定的时间范围内运行,另一种方法是每天运行。但是,当我将其设置为每天运行时,我只希望它在工作日继续运行。现在,从研究中我已经看到,您可以像这样使用Pandas遍历工作日:

I currently have a program setup to run two different ways. One way is to run over a specified time frame, and the other way is to run everyday. However, when I have it set to run everyday, I only want it to continue if its a business day. Now from research I've seen that you can iterate through business days using Pandas like so:

start = 2016-08-05
end = datetime.date.today().strftime("%Y-%m-%d")

for day in pd.bdate_range(start, end):
    print str(day) + " is a business Day"

当我运行

但是当我想让程序每天运行时,我不太想知道如何测试某一天是否是营业日。基本上我想做这样的事情:

But when I want to have the program ran everyday, I can't quite figure out how to test one specific day for being a business day. Basically I want to do something like this:

start = datetime.date.today().strftime("%Y-%m-%d")
end = datetime.date.today().strftime("%Y-%m-%d")

if start == end:
    if not Bdate(start)
        print "Not a Business day"

我知道我可能会设置pd.bdate_range()来执行我要寻找的操作,但我认为这会很草率且不直观。我觉得必须对此有一个更简单的解决方案。有建议吗?

I know I could probably setup pd.bdate_range() to do what I'm looking for, but in my opinion would be sloppy and not intuitive. I feel like there must be a simpler solution to this. Any advice?

推荐答案

由于 len pd.bdate_range()告诉我们在提供的日期范围内有多少个工作日,我们可以将其强制转换为 bool 来确定一天的范围是一个工作日:

Since len of pd.bdate_range() tells us how many business days are in the supplied range of dates, we can cast this to a bool to determine if a range of a single day is a business day:

def is_business_day(date):
    return bool(len(pd.bdate_range(date, date)))

这篇关于确定一天是否是Python / Pandas中的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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