Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

Pandas date_range()

Pandas date_range() is a function used to generate date ranges from start and end dates. This can be useful when analyzing datasets that contain temporal data such as stock prices over time, or social media clicks by day, or a company’s sales figures over months.

Here is a very basic program to illustrate how date_range() works:

import pandas as pd

datevalues = pd.date_range(start ='01/01/2023',
            end ='12/31/2023')

print(datevalues)

We first import pandas (“as pd”) and then call the date_range() function with a start and end dates. Finally we print the returned values (i.e., dates). The output in this case is:

DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', 
'2023-01-04', '2023-01-05', '2023-01-06', '2023-01-07', 
'2023-01-08', '2023-01-09', '2023-01-10',
...
'2023-12-22', '2023-12-23', '2023-12-24', '2023-12-25',
'2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29',
'2023-12-30', '2023-12-31'],
dtype='datetime64[ns]', length=365, freq='D')

Note that date_range() by default uses a daily frequency (specified in the last line of the output, as freq=’D’). We also see that this gives rise to 365 values since there are 365 days in a year. If we wish a different frequency, we can update our call giving a specific freq parameter:

import pandas as pd

datevalues = pd.date_range(start ='01/01/2023',
            end ='12/31/2023', freq='M')

print(datevalues)

The output will be:

DatetimeIndex(['2023-01-31', '2023-02-28', '2023-03-31', 
'2023-04-30', '2023-05-31', '2023-06-30', '2023-07-31', 
'2023-08-31', '2023-09-30', '2023-10-31', '2023-11-30', 
'2023-12-31'],
dtype='datetime64[ns]', freq='M')

Note that date_range() is cognizant of the fact that different months have different numbers of days. The output has 12 values, all situated at the end of the respective month. This is because ‘M’ denotes end of the month. If you would like the dates to be at the beginning of the month, use ‘MS’ as the value for frequency (for ‘month start’). Thus if we try:

import pandas as pd

datevalues = pd.date_range(start ='01/01/2023',
            end ='12/31/2023', freq='MS')

print(datevalues)

we will get:

DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01', 
'2023-04-01', '2023-05-01', '2023-06-01', '2023-07-01', 
'2023-08-01', '2023-09-01', '2023-10-01', '2023-11-01', 
'2023-12-01'],
dtype='datetime64[ns]', freq='MS')

as expected.

You can try to create a date range at a biweekly interval (perhaps you are trying to simulate pay dates):

Pandas date_range()

import pandas as pd

datevalues = pd.date_range(start ='01/01/2023',
            end ='12/31/2023', freq='15D')

print(datevalues)

The output will be:

DatetimeIndex(['2023-01-01', '2023-01-16', '2023-01-31', 
'2023-02-15', '2023-03-02', '2023-03-17', '2023-04-01', 
'2023-04-16', '2023-05-01', '2023-05-16', '2023-05-31', 
'2023-06-15', '2023-06-30', '2023-07-15', '2023-07-30', 
'2023-08-14', '2023-08-29', '2023-09-13', '2023-09-28', 
'2023-10-13', '2023-10-28', '2023-11-12', '2023-11-27', 
'2023-12-12', '2023-12-27'],
dtype='datetime64[ns]', freq='15D')

Note that we obtain 25 pay dates for the year 2023. A better way to do it is to use the in-built frequency called ‘SMS’ (which stands for: semi-month start frequency (1st and 15th of each month)).

Thus if we try:

import pandas as pd

datevalues = pd.date_range(start ='01/01/2023',
            end ='12/31/2023', freq='SMS')

print(datevalues)

we will get:

DatetimeIndex(['2023-01-01', '2023-01-15', '2023-02-01', 
'2023-02-15', '2023-03-01', '2023-03-15', '2023-04-01', 
'2023-04-15', '2023-05-01', '2023-05-15', '2023-06-01', 
'2023-06-15', '2023-07-01', '2023-07-15', '2023-08-01', 
'2023-08-15', '2023-09-01', '2023-09-15', '2023-10-01', 
'2023-10-15', '2023-11-01', '2023-11-15', '2023-12-01', 
'2023-12-15'],
dtype='datetime64[ns]', freq='SMS-15')

Note that this time we obtain 24 dates, not 25.

Below is an exhaustive list of the frequencies you can use:

B     business day frequency
C     custom business day frequency
D     calendar day frequency
W     weekly frequency
M     month end frequency
SM    semi-month end frequency (15th and end of month)
BM    business month end frequency
CBM   custom business month end frequency
MS    month start frequency
SMS   semi-month start frequency (1st and 15th)
BMS   business month start frequency
CBMS  custom business month start frequency
Q     quarter end frequency
BQ    business quarter end frequency
QS    quarter start frequency
BQS   business quarter start frequency
A, Y  year end frequency
BA, BY business year end frequency
AS, YS year start frequency
BAS, BYS business year start frequency
BH    business hour frequency
H     hourly frequency
T     minutely frequency
S     secondly frequency
L     milliseconds
U     microseconds
N     nanoseconds

So, to summarize, Pandas’s date_range() function allows you to quickly generate lists of dates with ease - whether they need specific start/end points or relative offsets between them! So if you're looking for an easy way to generate lists of dates - give date_range in Pandas a try!

For more about dates, checkout our blogpost on comparing dates in Python.

If you liked this blogpost, checkout our blogpost on returning a specific row or set of rows from a Pandas data frame.

Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!

Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.