data science

Create a range of dates using Pandas – Python

Here is how to create a range of dates using the Pandas module.

The range will start from April 2 2014 and will end October 1 2014.

Well…here it is

# import datetime so that we can format 
# the date to what we want
import pandas as pd
from datetime import datetime

# start and end dates
str_start = '2014-04-02'
str_end = '2014-10-01'

# create the range using the pandas function
pd_date_range = pd.date_range(str_start,str_end)

# now we can print it in the yyyy-mm-dd format.
for pd_date in pd_date_range:
    print pd_date.strftime('%Y-%m-%d')

That’s pretty much it!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s