确定一天是否是 Python / Pandas 中的工作日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39067626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if a day is a business day in Python / Pandas
提问by Justin
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:
我目前有一个程序设置来运行两种不同的方式。一种方法是在指定的时间范围内运行,另一种方法是每天运行。但是,当我将它设置为每天运行时,我只希望它在工作日继续运行。现在从研究中我看到你可以像这样使用 Pandas 迭代工作日:
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"
And this works great when I run my program over the specified period.
当我在指定的时间段内运行我的程序时,这很有效。
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"
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?
我知道我可能可以设置 pd.bdate_range() 来做我正在寻找的东西,但在我看来会很草率而且不直观。我觉得必须有一个更简单的解决方案。有什么建议吗?
回答by Zev Averbach
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:
由于len
ofpd.bdate_range()
告诉我们在提供的日期范围内有多少个工作日,我们可以将其转换为 abool
以确定某一天的范围是否为工作日:
def is_business_day(date):
return bool(len(pd.bdate_range(date, date)))
回答by Quickbeam2k1
I just found a different solution to this. This might be interesting if you want to find the next business day if your date is not a business day.
我刚刚找到了一个不同的解决方案。如果您的日期不是工作日,如果您想查找下一个工作日,这可能会很有趣。
bdays=BDay()
def is_business_day(date):
return date == date + 0*bdays
adding 0*bdays
rolls forward on the next business day including the current one. Unfortunately, subtracting 0*bdays
does not roll backwards (at least with the pandas version I was using)
0*bdays
在包括当前工作日在内的下一个工作日添加前滚。不幸的是,减法0*bdays
不会倒退(至少在我使用的Pandas版本中)
回答by Ian Thompson
Using at least numpy
version 1.7.0., try np.is_busday()
至少使用numpy
1.7.0. 版本,试试np.is_busday()
start = datetime.date.today().strftime("%Y-%m-%d")
end = datetime.date.today().strftime("%Y-%m-%d")
if start == end:
# added code here
if not np.is_busday(start):
print("Not a Business day")
回答by gioxc88
for me I use an old trick from Excel:
对我来说,我使用 Excel 中的一个老技巧:
from pandas.tseries.offsets import Day, BDay
def is_bday(x):
return x == x + Day(1) - BDay(1)
回答by Dinesh Pundkar
Please check this module - bdateutil
请检查此模块 - bdateutil
Please check the below code using above module :
请使用上述模块检查以下代码:
from bdateutil import isbday
from datetime import datetime,date
now = datetime.now()
val = isbday(date(now.year, now.month, now.day))
print val
Please let me know if this help.
如果这有帮助,请告诉我。