Skip to content

Instantly share code, notes, and snippets.

@christlc
Created October 28, 2016 07:51
Show Gist options
  • Save christlc/112a942cd3425dd31d1c5d4f26260eb3 to your computer and use it in GitHub Desktop.
Save christlc/112a942cd3425dd31d1c5d4f26260eb3 to your computer and use it in GitHub Desktop.
Date Hierarchy for OLAP
import pandas as pd
import numpy as np
START_YEAR = 2007
END_YEAR = pd.datetime.today()
def main():
df = pd.DataFrame()
df['Day'] = pd.date_range(str(START_YEAR), str(END_YEAR))
df['Month'] = df.Day.map(lambda x: "%d-%02d" % (x.year, x.month))
df['Quarter'] = df.Day.map(lambda x: "%d Q%d" % (x.year, x.quarter))
df['Year'] = df.Day.map(lambda x: str(x.year))
dow_lookup = ['1 Monday', '2 Tuesday', '3 Wednesday', '4 Thursday', '5 Friday', '6 Saturday', '7 Sunday']
df['Day_of_Week'] = df.Day.map(lambda x: x.dayofweek).map(lambda x: dow_lookup[x])
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment