
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return Period Object as Timestamp with Monthly Frequency in Python Pandas
To return the Period object as a timestamp with monthly frequency, use the period.to_timestamp() method and set the freq parameter as ‘M’.
At first, import the required libraries −
import pandas as pd
The pandas.Period represents a period of time. Creating a Period object
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)
Display the Period object
print("Period...\n", period)
Return the Timestamp representation of the Period object. We have set the frequency using the "freq" parameter. The frequency is set as 'M' i.e. monthly
print("\nPeriod to Timestamp with monthly (month-end) frequency...\n", period.to_timestamp(freq='M'))
Example
Following is the code
import pandas as pd # The pandas.Period represents a period of time # Creating a Period object period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45) # display the Period object print("Period...\n", period) # Return the Timestamp representation of the Period object # We have set the frequency using the "freq" parameter # The frequency is set as 'M' i.e. monthly print("\nPeriod to Timestamp with monthly (month-end) frequency...\n", period.to_timestamp(freq='M'))
Output
This will produce the following code
Period... 2021-09-18 17:20:45 Period to Timestamp with monthly (month-end) frequency... 2021-09-30 00:00:00
Advertisements