How to Dynamically Generate a Date Dimension Table using Python Pandas

The Pandas package in Python allows you to generate a list of dates dynamically and then extract their attributes with various datetime functions.

Here is how you can achieve this:

  1. Pandas date_range() function allows you to dynamically create a date range
  2. Date attributes can be extracted from the date columns using various datetime functions such as df['date'].dt.year
df = pd.DataFrame(pd.date_range('1/1/2021','12/31/2021'), columns=['date'])

df['year'] = df['date'].dt.year
df['quarter_number'] = df['date'].dt.quarter
df['quarter_text'] = df['date'].apply(lambda x: f'Q{x.quarter} {x.strftime("%Y")}')
df['month'] = df['date'].dt.month
df['year_month'] = df['date'].dt.strftime("%B %Y")
df['week'] = df['date'].dt.isocalendar().week
df['year_week'] = df['date'].apply(lambda x: f'{x.isocalendar()[0]}, Week {x.isocalendar()[1]}')
df['week_start'] = df['date'].dt.to_period('W').apply(lambda x: x.start_time)
df['week_end'] = df['date'].dt.to_period('W').apply(lambda x: x.end_time).dt.date
df['week_range'] = df.apply(lambda x: f"""{x['week_start'].strftime("%b %d, %Y")} to {x['week_end'].strftime("%b %d, %Y")} (Week {x['week']})""" , axis=1)
df['abbreviated_weekday'] = df['date'].dt.strftime("%a")
df['weekday'] = df['date'].dt.strftime("%A")

df

The resulting Pandas DataFrame looks like this:

Exporting the DataFrame to JSON can be achieved with this line of code: df.to_dict(orient='records')

Final Thoughts

Check out more Python tricks in this Colab Notebook or in my recent Python Posts.

Thanks for reading!


Posted

in

by

Tags: