Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

Automate calendar marking using python
Latest   Machine Learning

Automate calendar marking using python

Last Updated on July 26, 2023 by Editorial Team

Author(s): Yudhi Chen

Originally published on Towards AI.

Programming

How to create a custom calendar using python module July

Photo by Towfiqu barbhuiya on Unsplash

Introduction

Starting a new year, we always come up with a new resolution, vacation plan, or any important event that may come ahead. We mark all special occasions in the calendar to remind us of those things. Some people mark their calendar in the apps or some still got their physical calendar put on the desk and marked it for better visual publicly. The calendar we found might have a different color for weekends and generic public holidays and it depends on which country or even state where you live in.

Why customize?

In my case, I’m stationed in a country where the public holiday (PH) is state-dependent. It is good if I can get a physical calendar with PH marked. In the workplace, we also tend to have an ‘extra’ calendar of an important event, i.e. project related, company event, or even birthday list of our colleague. Using python module July, we can automate the marking, ready-for-print custom calendar to mark any important dates listed on the particular year.

Pre-requisite

First thing you need if you want to generate your own customized calendar is the list of the important date. I’m using the list of public holidays in my country (state) for demonstration using Jupyter Notebook. You can find the code details on my github and a sample of the public holiday list in CSV format as well.

Public Holiday List U+007C Image by Author

Step-1: install package → import → read data

The main module we are going to use is July. Ensure you install the module before proceeding to the next step. Once you manage to install, proceed to import the package. We also need pandas to process our data. Make sure you call it as well before proceeding further.

#!pip install july
import pandas as pd
import july
from july.utils import date_range

Once you manage to install, proceed to import the package. We also need panda to process our data. Make sure you call it as well before proceeding further. Using the pandas module, don’t forget to parse the date on the column Date.

# Read csv file contain list of public holiday
df = pd.read_csv('2022_my_ph.csv',parse_dates=['Date'])
df.head(3)

Step-2: process the data

The idea in this step is to generate a list of dates throughout the year, ie. 2022, and note the important date on df. As a typical calendar, another marking is also made on weekends (Saturday and Sunday). Hence, we will have two different marks: public holiday and weekend.

df1shall cover a list of dates starting from Jan 1st to Dec 31st with frequency daily as in ‘d’ while another list df1bshall cover the only business day as in ‘B’.

You can find more information offset aliases if you wish to create another data frame using different time frequency.

# Create list date range in year 2022
df1 = pd.date_range("2022-01-01","2022-12-31",freq='d')

# Create list date range in year 2022 ONLY for business day
df1b = pd.date_range("2022-01-01","2022-12-31",freq='B')

Next line, df2data frame create contain a collection of zeros list for 365 days and we will integrate both df1as index date and df2as data valuein a new data frame df3.

# Create list with value zero (0)
df2 = [0] * 365
# Create data frame df3
df3 = pd.DataFrame(data = df2, index = df1, columns = ['value'])

In the new data frame df3, we will update initial value (zero) at particular public holiday date listed in df1 using indexing property iloc. A similar update is also to be done for marking the weekend.

Note: there’s negate expression ~ since initially we have a list of business day df1b. You might think that this method will mask the public holiday if it falls on weekend (Saturday and Sunday). You are correct and it depends on how we utilize it.

So that’s it. We are ready to plot the calendar using a data frame df3.

# Encode the date according to public holiday listed at df
# Update value of df3 in which the df3 index = df['Date']
df3.iloc[df3.index.isin(df['Date'])] = 1

# Update value of df3 in which the df3 index is not same as df1b.
# All weekend, ie. Saturday and Sunday shall be given value '2'
df3.iloc[~df3.index.isin(df1b)] = 2

Step-3: plot the calendar

There are two kinds of calendars we can generate using module July, either we plot a typical calendar or another version of the calendar like what you see in a typical GitHub profile denote our contribution activity. My personal preference, I find the typical calendar is more than sufficient for me with a different color on public holidays and weekends.

# Calendar plot. 
july.calendar_plot(df3.index, df3['value'],
cmap = 'july',
date_label = True,
figsize = None)
Calendar 2022 U+007C Image by Author

For some applications, a calendar with GitHub style also might fit for purpose. Again, it all depends on how we use it. Another thing to highlight, this module July was made upon matplotlib module. Similar tweaks on parameters can be applied to this code, like cmap on what color you prefer to mark your calendar.

july.heatmap(dates=df3.index, 
data=df3['value'],
cmap='Pastel1_r',
month_grid=True,
horizontal=True,
value_label=False,
date_label=True,
weekday_label=True,
month_label=True,
year_label=True,
colorbar=False,
fontfamily="monospace",
fontsize=8,
title=None,
titlesize='large',
dpi=100)
Calendar 2022 with github style U+007C Image by Author

Conclusion

In this article, we have covered simple code to automate your marking calendar using the python module July. I hope you find it useful and beneficial to remind any special occasion of your daily activity.

Thanks for reading. Feel free to fork and tweak the code in this Github repo to suit your situation.

Reference

[1] python module July

I like to share useful knowledge in data analytics and see how people can benefit from it. Follow me on Medium and let me know if you have any constructive input and how to improve through comment. You can get in touch with me through Linkedin or Twitter. All the best on your learning endeavorU+1F44D!

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓