
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Convert given Timestamp to Period with quarterly frequency
To convert given Timestamp to Period, use the timestamp.to_period() method. Within that, set the frequency using the freq parameter. For quarterly frequency, set freq as Q.
At first, import the required libraries −
import pandas as pd
Create the timestamp object in Pandas
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
Convert timestamp to Period. We have set the frequency as quarterly using the "freq" parameter with value 'Q'
timestamp.to_period(freq='Q')
Example
Following is the code
import pandas as pd # set the timestamp object in Pandas timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33) # display the Timestamp print("Timestamp...\n", timestamp) # convert timestamp to Period # we have set the frequency as quarterly using the "freq" parameter with value 'Q' print("\nTimestamp to Period with quarterly frequency...\n", timestamp.to_period(freq='Q'))
Output
This will produce the following code
Timestamp... 2021-09-18 11:50:20.000033 Timestamp to Period with quarterly frequency... 2021Q3
- Related Articles
- Python Pandas - Convert given Timestamp to Period with minutely frequency
- Python Pandas - Convert given Timestamp to Period with weekly frequency
- Python Pandas - Convert given Timestamp to Period with monthly frequency
- Python Pandas - Convert given Timestamp to Period with hourly frequency
- Python Pandas - Convert given Timestamp to Period
- Python Pandas - Convert Period to desired frequency
- Python Pandas - Return the Period object as a timestamp with monthly frequency
- Python Pandas - Return the Period object as a timestamp with minutely frequency
- Python Pandas - Return the Period object as a timestamp with daily frequency
- Python Pandas - Return the Period object as a timestamp with yearly frequency
- Python Pandas - Convert PeriodIndex object to Timestamp and set the frequency
- Python Pandas - Get the frequency for the given Period object
- Python Pandas - Change the frequency of the given Period object from Seconds to Daily frequency
- Python Pandas - Change the frequency of the given Period object from Seconds to Hourly frequency
- Python Pandas - Change the frequency of the given Period object from Seconds to Minutely frequency

Advertisements