Open In App

Pandas – Generating ranges of timestamps using Python

Last Updated : 28 Feb, 2022
Like Article

A timestamp is a string of characters or encrypted or encoded data that identifies the time and date of an event, usually indicating the time and date of day, and is often accurate to a fraction of a second. timestamps are used to maintain track of information. When information was created, transmitted, edited, or removed, it was given a timestamp. let’s demonstrate how to generate ranges of timestamps using python.

Timestamps are of the form:

general format

YYYY-MM-DD hh:mm:ss

  • Y stands for year
  • M stands for month
  • D  stands for day
  • h  stands for hour
  • m  stands for minutes
  • s  stands for seconds

Method 1: Using datetime.timedellta()

Import DateTime and pandas packages. We take a base date which is today’s date timedelta() increasing in steps of ‘1’ is added to today’s date using datetime.timedelta() method. timedelta() takes an argument day which is the number of days we should increment our base date. below is the code for generating timestamps for the next 10 days. we change the range as per requirement.


# importing packages
import datetime
import pandas as pd
n_days = 10
# today's date in timestamp
base =
# calculating timestamps for the next 10 days
timestamp_list = [base + datetime.timedelta(days=x) for x in range(n_days)]
# iterating through timestamp_list
for x in timestamp_list:


Method 2: Using pd.date_range() method

In this approach, we directly use the date_range() method from the panda’s library. The start date is datetime. today() which is today’s date. Periods is the number of periods to generate. We can directly generate a range of timestamps by using this method.


# importing packages
import pandas as pd
from datetime import datetime
# creating a range of timestamps
timestamp_list = pd.date_range(, periods=10).tolist()
for i in timestamp_list:


2022-02-20 08:28:35.822503
2022-02-21 08:28:35.822503
2022-02-22 08:28:35.822503
2022-02-23 08:28:35.822503
2022-02-24 08:28:35.822503
2022-02-25 08:28:35.822503
2022-02-26 08:28:35.822503
2022-02-27 08:28:35.822503
2022-02-28 08:28:35.822503
2022-03-01 08:28:35.822503
<class 'pandas._libs.tslibs.timestamps.Timestamp'>

Instead of using the ‘periods’ parameter in pd.date_range() method, we can directly use start and end parameters to specify the start and the end dates. The default frequency of this method is ‘day’ so we get the range() of dates incremented by 24 hours.


# importing packages
import pandas as pd
# range of timestamps
timestamp_range = pd.date_range(start="2020-02-20",
for i in date_timestamp:


2020-02-20 00:00:00
2020-02-21 00:00:00
2020-02-22 00:00:00
2020-02-23 00:00:00
2020-02-24 00:00:00
2020-02-25 00:00:00
2020-02-26 00:00:00
2020-02-27 00:00:00
2020-02-28 00:00:00
2020-02-29 00:00:00
2020-03-01 00:00:00
<class 'pandas._libs.tslibs.timestamps.Timestamp'>

Method 3: Using pd.period_range() method

The pd.period_range() is similar to pd.date_range() but it returns period index so we further need to use to_timestamp() method to change it into timestamp values. we use the freq parameter to set the frequency to months by using the string “M”. In the above-covered examples frequency was the day. A range of timestamps that are incremented by months is generated in this example.


# importing packages
import pandas as pd
import datetime
# range of dates
date_range = pd.period_range(, periods=10, freq='M')
# timestamp range
timestamp_range = [x.to_timestamp() for x in date_range]
# iterating through timestamp range
for i in timestamp_range:


2022-02-01 00:00:00
2022-03-01 00:00:00
2022-04-01 00:00:00
2022-05-01 00:00:00
2022-06-01 00:00:00
2022-07-01 00:00:00
2022-08-01 00:00:00
2022-09-01 00:00:00
2022-10-01 00:00:00
2022-11-01 00:00:00
<class 'pandas._libs.tslibs.timestamps.Timestamp'>

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads