How to create Pandas date week and month range objects?

In this tutorial we’ll learn lots of very useful information about using the Pandas data range objects. We’ll learn about two very versatile Pandas functions: data_range and bdate_range that allow to create data ranges with ease. This will come very handy when pre-process / wrangling your data before analysis and visualization.

Preparation

Before starting to work with Pandas, we obviously need to import it.

import pandas as pd

If the Pandas library is not installed in your Python Development environment, you will receive a Pandas module not found error that you can easily troubleshoot.

Define a date range between two dates

OK, let’s start by defining a Pandas date range between two dates. The following command will create a DateTimeIndex consisting of January 31 days. The syntax format is self explanatory – you need to specify the start and end dates of the range.

pd.date_range(start = '1/1/2022', end ='1/31/2022')

Weekly and Monthly date ranges in Pandas

The freq parameter helps to define the right frequency, in our case, it would be by week.

pd.date_range(start = '1/1/2022', end ='6/30/2022', freq='w')
#Every month
pd.date_range(start = '1/1/2022', end ='6/30/2022', freq='M')

We could have well achieve the same results by using the period parameter:

pd.date_range(start = '1/1/2022', periods=6, freq='M')

Start of the month date ranges

What if we would like to display a range consisting of each months’ first calendar date? We can easily accomplish that using the freq= ‘MS’ (month start) parameter.

pd.date_range(start = '1/1/2022', end ='6/30/2022', freq='MS')

Date range with custom frequencies

Let’s assume the we would like to define a range that contains the tenth day of every month. We’ll first define a range that starts with on the month first day and then use the shift() method of the DateTImeIndex to offset the number of days by 9 days.

pd.date_range(start = '1/1/2022', end ='6/30/2022', freq='MS').shift(9, freq = 'd')

By business days

What if we would like to define a range that contains only business dates (excludes weekends)? In this case our code will return the first 6 business dates of January 22.

pd.bdate_range(start = '1/1/2022', periods=6)

Export to list

In our last snippet for today, we’ll export our DataTimeIndex range to a list of timestamps. This is quite easy using the to_list() method:

pd.bdate_range(start = '1/1/2022', periods=6).to_list()

Convert dates to a DataFrame

data = pd.DataFrame(date_s, columns = ['transfer_date'])
print(data)

Write range as a Pandas column

In this more practical example, we’ll use our date_range as a DataFrame index to define a time series DataFrame:

date_s = pd.date_range(start = '1/1/2022', end ='6/30/2022', freq='MS').shift(9, freq = 'd')
sales_fig = { 'sales': [150,159,180,180,190, 140] }

data = pd.DataFrame(index= date_s, data=sales_fig )

print(data)

The result will be:

sales
2022-01-10150
2022-02-10159
2022-03-10180
2022-04-10180
2022-05-10190
2022-06-10140