import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create index of periods (as date_range only allows range of 584 years due to nanosecond resolution)
idx = pd.period_range('0001-01-01', '2020-12-31', freq='D')
# Create dataframe from index of periods. For weekday, Monday=0 and Sunday=6.
df = pd.DataFrame({'date': idx, 'weekday_num': idx.weekday, 'weekday': None, 'monthday': idx.day})
# Names are easier to with than numbers: map the day of week to weekday name
weekday_map_dict = {0: 'monday', 1: 'tuesday', 2: 'wednesday', 3: 'thursday', 4: 'friday', 5: 'saturday', 6: 'sunday'}
df['weekday'] = df['weekday_num'].map(weekday_map_dict)
daycombos = df.groupby(['weekday_num','monthday', 'weekday']).size().to_frame('frequency').reset_index().sort_values(by=['frequency'], ascending=False)
daycombos[['weekday', 'monthday', 'frequency']][daycombos.monthday.eq(13)]
daycombos[['weekday', 'frequency']][daycombos.monthday.eq(13)].plot.bar(x='weekday', y='frequency', legend=False, title='Frequency of the 13th by weekday')
# What are the most common day/date combinations?
daycombos[['weekday', 'monthday', 'frequency']].head(30)