Open In App

Pandas – Generating ranges of timestamps using Python

Last Updated : 28 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 pd.Timestamp.today(). 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.

Python3




# importing packages
import datetime
import pandas as pd
  
n_days = 10
  
# today's date in timestamp
base = pd.Timestamp.today()
  
# 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:
    print(x)


Output:

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.

Python3




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


Output:

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.

Python3




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


Output:

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.

Python3




# importing packages
import pandas as pd
import datetime
  
# range of dates
date_range = pd.period_range(
    start=datetime.datetime.today(), 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:
    print(i)
print(type(i))


Output:

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
Previous
Next
Share your thoughts in the comments

Similar Reads