{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Days of the Week of each Month through the Years\n", "### [Album Shen](http://albumshen.com/)\n", "*Wednesday the 21st, November, 2018*\n", "\n", "I've been helping my 5th grade sister with her math league problems, and some brainteasers involving leap years came up. This sparked a seemingly (probably actually is) trivial question of how do the days of the week compare to one another in how often they appear in each month of our calendars? One would guess that over time the frequency of each weekday occurring for each month would more-or-less get evened out, right? (i.e. an equal number of mon, tues, wed, etc.) Let's find out by taking a look at how the months on our calendar actually cycle through the days of the week, and when/if the sequence comes back to itself. Some questions we may answer are:\n", "\n", "* Over time, will there be more Mondays in January than Tuesdays in January?\n", "* How many Wednesdays have there been in March since the start of the Gregorian calendar?\n", "* If I host an event every 5th Thursday in February, would I be hosting more events than if I were to do it every 5th Friday?\n", "\n", "There are a number of [existing algorithms that can determine the day of the week](https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week) for any given date. But I wanted to see how the distribution of these days in each month of the year play out over the course of an entire Gregorian cycle. We'll be running a program to evaluating the frequency of each weekday as they've occur in each month throughout the years since our current calendar's conception.\n", "\n", "**Fun fact:** The first day of the [Gregorian calendar](https://en.wikipedia.org/wiki/Gregorian_calendar) was October 15, 1582. A *Friday*." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Setting up the libaries & functions we'll be using\n", "import pandas as pd\n", "from IPython.display import display_html, HTML\n", "from datetime import date, datetime, timedelta as td\n", "from plotly.grid_objs import Grid, Column\n", "import plotly.plotly as py\n", "import time\n", "\n", "# Dictionaries for dataframe column & row headers\n", "weekdays = {0:\"sun\", 1:\"mon\", 2:\"tues\", 3:\"wed\",\n", " 4:\"thurs\", 5:\"fri\", 6:\"sat\"}\n", "months = {0:\"jan\", 1:\"feb\", 2:\"mar\", 3:\"apr\",\n", " 4:\"may\", 5:\"jun\", 6:\"jul\", 7:\"aug\",\n", " 8:\"sept\", 9:\"oct\", 10:\"nov\", 11:\"dec\"}\n", "ordinal = {0:\"1st\", 1:\"2nd\", 2:\"3rd\", 3:\"4th\",\n", " 4:\"5th\", 5:\"6th\", 6:\"7th\"}\n", "\n", "# Print the dates + weekday information given the\n", "# intervals of days from a specified origin date\n", "def printWeekDays(daysFrom, originDate):\n", " DayOnes = pd.to_datetime(daysFrom, unit='D', origin=pd.Timestamp(originDate))\n", " for day in DayOnes:\n", " print (day.strftime(\"%Y %b %d: %A (Day %w)\"))\n", "\n", "# Display dataframes side-by-side with their names on top\n", "def disp_dfs(*args):\n", " html_str = ''\n", " for df in args:\n", " html_str += '
'\\\n", " +df.name+'
'\\\n", " +df.to_html()+'Distribution of Days in Each Month by Order of Appearance in Non-Leap Years
\n", " | 1st | \n", "2nd | \n", "3rd | \n", "4th | \n", "5th | \n", "6th | \n", "7th | \n", "
---|---|---|---|---|---|---|---|
jan | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
feb | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
mar | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
apr | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
may | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
jun | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
jul | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
aug | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
sept | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
oct | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
nov | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
dec | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
Distribution of Days in Each Month by Order of Appearance in Leap Years
\n", " | 1st | \n", "2nd | \n", "3rd | \n", "4th | \n", "5th | \n", "6th | \n", "7th | \n", "
---|---|---|---|---|---|---|---|
jan | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
feb | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
mar | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
apr | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
may | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
jun | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
jul | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
aug | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
sept | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
oct | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
nov | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
dec | \n", "5 | \n", "5 | \n", "5 | \n", "4 | \n", "4 | \n", "4 | \n", "4 | \n", "
Number of Weekdays That Occur in Each Month in Each Gregorian Calendar Cycle
\n", " | sun | \n", "mon | \n", "tues | \n", "wed | \n", "thurs | \n", "fri | \n", "sat | \n", "
---|---|---|---|---|---|---|---|
jan | \n", "1772 | \n", "1770 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1772 | \n", "1771 | \n", "
feb | \n", "1613 | \n", "1615 | \n", "1613 | \n", "1615 | \n", "1613 | \n", "1614 | \n", "1614 | \n", "
mar | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1770 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "
apr | \n", "1714 | \n", "1715 | \n", "1714 | \n", "1715 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "
may | \n", "1772 | \n", "1770 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1772 | \n", "1771 | \n", "
jun | \n", "1714 | \n", "1715 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "1715 | \n", "
jul | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1770 | \n", "
aug | \n", "1771 | \n", "1772 | \n", "1770 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1772 | \n", "
sept | \n", "1715 | \n", "1714 | \n", "1715 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "
oct | \n", "1770 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "
nov | \n", "1715 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "1714 | \n", "1715 | \n", "1714 | \n", "
dec | \n", "1771 | \n", "1772 | \n", "1772 | \n", "1771 | \n", "1772 | \n", "1770 | \n", "1772 | \n", "