How to create Pandas date 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 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')

Date range consisting of months

We will again use the freq parameter to specify the range frequency.

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

Note that the range is made from the last day of every month.

DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30'],
              dtype='datetime64[ns]', 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')

And the result is as expected:

DatetimeIndex(['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
               '2022-05-01', '2022-06-01'],
              dtype='datetime64[ns]', 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 od 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 retuen the first 6 business dates of January 22.

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

Write the range 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 date range to 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