How to use pd.period_range() method In Python
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'>
Pandas – Generating ranges of timestamps using Python
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