{ "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", " '

'\\\n", " +df.name+'

'\\\n", " +df.to_html()+'

Distribution of Days in Each Month by Order of Appearance in Non-Leap Years

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
1st2nd3rd4th5th6th7th
jan5554444
feb4444444
mar5554444
apr5544444
may5554444
jun5544444
jul5554444
aug5554444
sept5544444
oct5554444
nov5544444
dec5554444

Distribution of Days in Each Month by Order of Appearance in Leap Years

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
1st2nd3rd4th5th6th7th
jan5554444
feb5444444
mar5554444
apr5544444
may5554444
jun5544444
jul5554444
aug5554444
sept5544444
oct5554444
nov5544444
dec5554444

Number of Weekdays That Occur in Each Month in Each Gregorian Calendar Cycle

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sunmontueswedthursfrisat
jan1772177017721771177217721771
feb1613161516131615161316141614
mar1772177117721770177217711772
apr1714171517141715171417141714
may1772177017721771177217721771
jun1714171517141714171417141715
jul1772177117721772177117721770
aug1771177217701772177117721772
sept1715171417151714171417141714
oct1770177217711772177217711772
nov1715171417141714171417151714
dec1771177217721771177217701772