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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
The Pandas package provides a convenient solution for this:
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).
There's a fairly decent book on this out there you may want to check out: Calendrical Calculations
Use the holiday library in python.
For USA holiday:
1. To check a date holiday or not.
2. To list out all holidays in US:
You can find holidays for any country you like the list of countries listed on my Blog. My Blog on Holidays
Github Link of Holidays Python
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:
The USA doesn't have Easter-related holidays. Definition is not difficult:
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):
I'm sure there are ways of specifying holidays that aren't catered for by the above rules ... any such news is welcome.
Make sure date != one of these:
http://www.buyusa.gov/uk/en/us_bank_holidays.html