In this tutorial, we’ll try to have a look at different methods used to create a date range in pandas with different functionalities.
How to Create a date range?
We can create date ranges with function pandas.date_range()
which is a built-in function in pandas
Basic Syntax:
import pandas as pd pd.date_range(start, end, periods, freq, …)
start: The start date
end: The end date
periods: The number of periods to generate
freq: The frequency to use (Refer to this link to know more about it)
Using this inbuilt function we can generate the data ranges in 3 different patterns:
- Data range with individual dates
- Date Range with Specific Number of Periods
- Date Range with Specific Frequency
Method -1: Data range with individual dates

In this method, we give the start date and end date and generate all the individual dates between them.
Code:
pd.date_range(start='5/5/1995', end='5/15/1995')
This gives the date ranges containing all the 10 dates between the start date and end date
Method -2: Date Range with Specific Number of Periods
In this method, we give the start date and the end date along with the period and generate dates that are of equally spaced periods from the start date to the end date.
Code:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
pd.date_range(start='5/5/1995', end='5/15/1995', periods=3)
This gives the date ranges containing 3 equally spaced days in the range from the start to the end date.
Method -3: Date Range with Specific Frequency
In this method, the below code shows how to create a date range that starts on a specific date and has a month-level frequency for six periods from the start date:
Code:
pd.date_range(start='1/1/2020', freq='MS', periods=6)
The above code gives a series of six dates that are each one month apart. Note that ‘MS‘ stands for ‘Month Start.’ You can find a complete list of date aliases here.
Examples
Now, let us try to have a look at examples of each method discussed above
Method – 1: Example
Code
import pandas as pd pd.date_range(start='5/5/1995', end='5/15/1995')
Output:
DatetimeIndex(['1995-05-05', '1995-05-06', '1995-05-07', '1995-05-08', '1995-05-09', '1995-05-10', '1995-05-11', '1995-05-12', '1995-05-13', '1995-05-14', '1995-05-15'], dtype='datetime64[ns]', freq='D')
Method – 2: Example
Code:
import pandas as pd pd.date_range(start='5/5/1995', end='5/15/1995', periods=3)
Output:
DatetimeIndex(['1995-05-05', '1995-05-10', '1995-05-15'], dtype='datetime64[ns]', freq=None)
Method – 3: Example
Code:
import pandas as pd pd.date_range(start='1/1/2020', freq='MS', periods=6)
Output:
DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-05-01', '2020-06-01'], dtype='datetime64[ns]', freq='MS')
Summary
In this tutorial, we looked at different methods used to create a date range in pandas.
We understood the different ways of using the built-in function pandas.date_range()
to create different date ranges.
- Data range with individual dates.
- Date Range with a specific number of periods.
- Date Range with a specific frequency.
You might also be interested in –
- Pandas – Get only date from datetime
- Pandas – Get Day of Week from Date
- Pandas – Change Format of Date Column
- Pandas – Extract Year from a datetime column
- Get Quarter from Date in Pandas
- Remove Time from Date in Pandas
- Pandas – Category Column with Datetime Values
- Pandas – Get Month from Date
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.