Saturday, October 25, 2025
HomeLanguagesPython Pandas – Convert PeriodIndex object to Timestamp and set the frequency

Python Pandas – Convert PeriodIndex object to Timestamp and set the frequency

In this article, we will discuss how to convert period index objects to timestamp and set the frequency in the python programming language.

The pandas PeriodIndex.to_timestamp() method is used to convert a PeriodIndex object to Timestamp and set the frequency. frequency can be set using the ‘freq’ parameter of the method.

Example 1:

Pandas package is imported. A period index object is created using pd.PeriodIndex() function where we pass in an array of DateTime values and frequency is specified as “year”. The period index object will have YearEnd type frequency. PeriodIndex object is converted to timestamp object by using the pd.to_timestamp() method. 

Python3




# import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents year.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'],
                             freq="Y")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp())


Output:

Output

Example 2:

In this example, we give the string “M” as the frequency which gives us a period index object of type “MonthEnd”. we also specify the time stamp object to have a frequency of “M”.

Python3




#import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents month.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'], 
                             freq="M")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp(freq='M'))


Output:

Output

Example 3:

In this example, we give the string “D” as the frequency which gives us a period index object of type “Day”. we also specify the time stamp object to have a frequency of “D”. The period Index object values match exactly with the values in the Timestamp object as we specify frequency to be “Day”.

Python3




# import packages
import pandas as pd
  
# Create a PeriodIndex object
# freq ='Y' represents Day.
periodIndex = pd.PeriodIndex(['2022-12-21 09:30:20', '2021-11-20 06:45:40',
                              '2020-10-19 03:38:15', '2019-09-18 01:30:30'],
                             freq="D")
  
print('period index object : ' + str(periodIndex))
print("frequency of the periodIndex object : ", periodIndex.freq)
  
# Display PeriodIndex frequency as string
print("frequency object as a string : ", periodIndex.freqstr)
  
# Converting PeriodIndex object to timestamp
print("Timestamp object : ", periodIndex.to_timestamp(freq='D'))


Output:

Output

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS