python 检测美国假期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2394235/
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
Detecting a US Holiday
提问by Cerin
What's the simplest way to determine if a date is a U.S. bank holiday in Python? There seem to be various calendars and webservices listing holidays for various countries, but I haven't found anything specific to banks in the U.S.
在 Python 中确定日期是否为美国银行假日的最简单方法是什么?似乎有各种日历和网络服务列出了各个国家/地区的假期,但我没有找到任何特定于美国银行的内容
采纳答案by John Machin
Some general comments:
一些一般性评论:
I don't think that @ast4 really means "nth day of nth week of nth month algorithm". The notion of "nth week in nth month" is mind-snapping (like the "ISO calendar"). I've never seen a holiday defined in terms of "nth week". Martin Luther King Day is an example of the"Nth weekday in month" type of holiday:
我不认为@ast4 真的意味着“第 n 个月算法的第 n 周的第 n 天”。“第 n 个月中的第 n 周”的概念令人震惊(如“ISO 日历”)。我从未见过以“第 n 周”来定义的假期。马丁路德金纪念日是“一个月中的第 N 个工作日”类型假期的一个例子:
MONDAY, ...., SATURDAY = range(7)
JAN, ....., DEC = range(1, 12)
Holiday("Martin L King's Birthday", type='floating',
ordinal=3, weekday=MON, month=JAN)
Holiday("Memorial Day", type='floating',
ordinal=-1, weekday=MON, month=MAY)
The USA doesn't have Easter-related holidays. Definition is not difficult:
美国没有与复活节相关的假期。定义并不难:
Holiday("Good Friday", type='moveable',
base='gregorian_easter', delta_days=-2)
Holiday("Easter Monday", etc, delta_days=1)
# Some states in Australia used to have Easter Tuesday (no kidding)
Holiday("Easter Tuesday", etc, delta_days=2)
The 'base' idea can be used to cater for lunar new year, in fact any holiday that is an offset from a base date that needs a special procedure to calculate it.
“基准”的想法可用于迎合农历新年,实际上任何与基准日期有偏差的假期都需要特殊程序来计算。
The so-called "static" holidays are not fixed when the "fixed" date is a Saturday or Sunday and may even vanish (no alternative day off work):
当“固定”日期是星期六或星期日时,所谓的“静态”假期是不固定的,甚至可能会消失(没有其他休息日):
# Americans will get a day off on Friday 31 Dec 2010
# because 1 Jan 2011 is a Saturday.
Holiday("New Year's Day", type='fixed',
day=1, month=JAN, sat_adj=-1, sun_adj=????)
# Australia observes ANZAC Day on the day, with no day off
# if the fixed date falls on a weekend.
Holiday("ANZAC Day", type='fixed', day=25, month=APR, sat_adj=0, sun_adj=0)
# Two consecutive "fixed" holidays is OK; three would need re-thinking.
# Australia again:
Holiday("Christmas Day", type='fixed', day=25, month=DEC, sat_adj=2, sun_adj=1)
Holiday("Boxing Day", type='fixed', day=26, month=DEC, sat_adj=2, sun_adj=2)
I'm sure there are ways of specifying holidays that aren't catered for by the above rules ... any such news is welcome.
我确信有一些方法可以指定上述规则不满足的假期……欢迎任何此类消息。
回答by user2235214
The Pandaspackage provides a convenient solution for this:
的Pandas包为此提供了一个方便的解决方案:
from pandas.tseries.holiday import USFederalHolidayCalendar
cal = USFederalHolidayCalendar()
holidays = cal.holidays(start='2014-01-01', end='2014-12-31').to_pydatetime()
if datetime.datetime(2014,01,01) in holidays:
print True
回答by shaurya uppal
Use the holiday library in python.
在 python 中使用假日库。
pip install holidays
pip 安装假期
For USA holiday:
美国假期:
1. To check a date holiday or not.
1. 检查日期假期与否。
from datetime import date
import holidays
# Select country
us_holidays = holidays.US()
# If it is a holidays then it returns True else False
print('01-01-2018' in us_holidays)
print('02-01-2018' in us_holidays)
# What holidays is it?
print(us_holidays.get('01-01-2018'))
print(us_holidays.get('02-01-2018'))
2. To list out all holidays in US:
2. 列出美国的所有假期:
from datetime import date
import holidays
# Select country
us_holidays = holidays.US()
# Print all the holidays in US in year 2018
for ptr in holidays.US(years = 2018).items():
print(ptr)
You can find holidays for any country you like the list of countries listed on my Blog. My Blog on Holidays
您可以在我的博客上列出的国家/地区列表中找到您喜欢的任何国家/地区的假期。 我的假期博客
回答by Tom R
回答by ast4
I've actually worked recently on a problem much like this one. The static holidays are rather trivial to generate (e.g. New Years Eve - December 31st, (cycle through years)). There are well defined algorithms out there to generate the floating holidays. Essentially you have a starting date (e.g. January 1st, 1900) and work from there. What I ended up implementing was a nth day of nth week of nth month algorithm (e.g. MLK day = 3rd Monday of January). Easter is a bit different to do, but again there are well defined algorithms for that already out there (Good Friday is trivial after you have January).
实际上,我最近一直在研究一个与此类似的问题。静态假期的生成相当简单(例如新年前夜 - 12 月 31 日,(逐年循环))。有定义明确的算法可以生成浮动假期。基本上,您有一个开始日期(例如 1900 年 1 月 1 日)并从那里开始工作。我最终实现的是第 n 个月算法的第 n 周的第 n 天(例如 MLK 日 = 1 月的第三个星期一)。复活节有点不同,但同样有明确定义的算法已经存在(耶稣受难日在一月之后是微不足道的)。
There's a fairly decent book on this out there you may want to check out: Calendrical Calculations
有一本相当不错的书,你可能想看看:日历计算