5 Minute Guide to Detecting Holidays in Python | by Dario Radečić | Towards Data Science

If you decide to dive deep into data analysis and want to, for example, analyze purchasing trends, you’ll probably want to construct a new attribute in your dataset having only two possible outcomes — 0 or 1 — is a holiday, isn’t a holiday.
Take a moment to think about why this derived variable might be useful. You probably won’t uncover a greater volume of purchases on holidays itself, but how did the sales do in the week leading to the holiday?
A whole another animal, isn’t it?
Anyway, in today’s rather quick post I want to discuss one Python library I’ve found some time ago — holidays — yes, that’s the name. It allows you to get dates and names of major holidays for specific year(s) and for a specific country. You might be wondering now how many countries does it support, and the answer will positively surprise you — around 50-ish (with the ability to specify individual regions for some countries).
You can find the whole list here:
pypi.org
The goal of this article is to do some exploration of the library and showcase functions you’ll use most often. We’ll create a list of dates for the whole year of 2019, and from there declare another column which will have the value of 1 if there is (or was) a holiday on that date, and 0 otherwise.
Before jumping into code you’ll need to install the library, so fire up the terminal and execute the following:
pip install holidays
Is everything good? I mean it should, it’s only a pip install for Pete’s sake. Nevertheless, let’s do some real stuff now.
Ass always, your Notebook should start with imports. Nothing special here, only Numpy, Pandas, Datetime, and yeah, newly installed Holidays library:
You probably won’t need Numpy to follow along, but I always like to have it imported.
If you open the documentation of Holidays library and scroll down a bit, you’ll see a list of 50-ish supported countries.
It’s now very easy to extract holidays, you’ll need to loop over the holidays in the country of interest, and also specify the year(s) of interest:
Yeah, it’s that easy. The dates obtained, however, aren’t in desired format. Most of the time you’ll care only the date itself, not what holiday it represents — as you’ll most like have a binary attribute in dataset having the value 1 if the holiday is present and 0 otherwise.
The good news is, it’s fairly easy to extract string representation of the dates:
Let’s now store all the string representations in a separate list — it will come in handy later:
That’s great. Now we can construct a Pandas DataFrame object and play around with those holidays.
With Pandas, it’s fairly straightforward to construct a list of dates, let’s say for the whole year of 2019:
Great. Now we can construct a DataFrame object from those dates — let’s put them into Dates column:
Now here comes a slight problem. The dates look to be stored in a string format, just like our us_holidays list, but that’s not the case. It’s a Timestamp object. The next cell verifies that claim:
So let’s explore how to extract only the date as a string, and discard the time information. You’ll want to convert Timestamp to a string, and then split the string and keep only the left portion (date information). It’s a fairly straightforward thing to do:
Now we can use the power of the list comprehensions to construct a binary list, having the value of 1 if the date is holiday and 0 otherwise. I’ve decided to wrap everything into print() and specify a different ending parameter, so the whole list can fit on screen:
And now, finally, by the same principle, you can add that list as a column in previously created Pandas DataFrame:
And you’re pretty much done. You can now proceed further with your analysis. The dataset you have will have more attributes, of course, so this new binary column might come in handy for analysis.
Thanks for reading, I hope you can somehow utilize this knowledge in your workflow.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.