- Thu 13 September 2018
- datascience
- #datetime, #calendar
After having an exam on my birthday last year, I am horrified at the thought of having another surprise birthday accounting exam. This got me wondering what days of the week my birthdays will fall on, and what years I will have a weekend birthday. This is what I'll be looking for in this quick analysis.
import calendar as cal
import datetime as dt
import pandas as pd
dt.date(1998,2,9).strftime('%A')
Apparently I was born on a Monday, which explains almost all of the variance in my dry sense of humor.
The following lines give the date and day of week that all my birthdays fall on until I can reasonably expect to live:
birthday = lambda year: dt.date(year, 2, 9)
df = pd.DataFrame({'year': range(2018, 2100)})
df['bday'] = pd.to_datetime(df.year.map(birthday))
df['dayofwk'] = df.bday.dt.strftime('%a')
df.head()
Huzzah! It looks like my next two birthdays will be on a weekend for my last two years of college.
A simple rule of thumb that I can see is that the day of the week my birthday falls on goes up by one each year, with the exception of leap years where it goes up by two.
FUN FACT: Leap years fall on the same years as the Summer Olympics.
The next block checks for if my birthday is on a weekend for a given year:
df['weekend'] = (df['dayofwk'] == 'Sat') | (df['dayofwk'] == 'Sun')
df.head()
Now I want to know how long I'll have to wait to have that sweet weekend birthday. I'll look for weekend epochs where the count is only increased on years where my birthday is on a weekend.
df['wknd_epoch'] = df.weekend.cumsum()
df.head(10)
This gives me a quick summary of the intervals I have to wait for a weekend birthday. The pattern seems to show that I get two weekend birthdays in a row followed by five that land on weekdays:
df[df['wknd_epoch'] > 0].groupby('wknd_epoch').year.agg(['size', 'min'])
#'size' returns wait time in years for next epoch and 'min' gives lowest
#'wknd_epoch' value, which happens to be years with a weekend birthday.
It looks like 2020 will be my last weekend birthday and begin an epoch of weekday birthdays. If I live to see my 100th birthday, it seems that I'll be able to celebrate on a weekend! Until then, I guess I'll be working on life.
That concludes my short analysis of my birthdays. Feel free to let me know your thoughts!