
- 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 - Format the Period object and display Quarter
To format the Period object, use the period.strftime() method and to display Quarter, set the parameter as Q%q.
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 = 8, minute = 20, second = 45)
Display the Period object
print("Period...\n", period)
Display the result. Here, Period object is formatted and Quarter is displayed
print("\nString representation (display quarter)...\n", period.strftime('Q%q'))
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 = 8, minute = 20, second = 45) # display the Period object print("Period...\n", period) # display the result # Here, Period object is formatted and Quarter is displayed print("\nString representation (display quarter)...\n", period.strftime('Q%q'))
Output
This will produce the following code
Period... 2021-09-18 08:20:45 String representation (display quarter)... Q3
- Related Articles
- Python Pandas - Format the Period object and display the Time with 24-Hour format
- Python Pandas - Format the Period object and display the Year without century
- Python Pandas - Get the quarter of the year from Period object
- Python Pandas - Display the quarter of the date from the PeriodIndex object
- Python Pandas - Format and return the string representation of the Period object
- Python Pandas - Get the year from the Period object
- Python Pandas - Display the end time of the period for each element in the given PeriodIndex object
- Python Pandas - Get the frequency for the given Period object
- Python Pandas - Return the Timestamp representation of the Period object
- Python Pandas - Format the string representation of the PeriodIndex object
- Python Pandas - Get the Day of the year from Period object
- Python Pandas - Find the end time for the given Period object
- Python Pandas - Find the start time for the given Period object
- Python Pandas - Display the start time of the custom business hour in 24h format from the BusinessHour offset object
- Python Pandas - Display the end time of the custom business hour in 24h format from the BusinessHour offset object

Advertisements