
- 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 and return the string representation of the Period object
To format and return the string representation of the Period object, use the period.strftime() method. With that, set the format specifiers as an argument like strftime('%d-%b-%Y').
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 formatted string representation
print("\nString representation (format)...\n", period.strftime('%d-%b-%Y'))
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 print("\nString representation (format)...\n", period.strftime('%d-%b-%Y')) # display the result print("\nString representation (format with different directives)...\n", period.strftime('%b. %d, %Y was a %A'))
Output
This will produce the following code
Period... 2021-09-18 08:20:45 String representation (format)... 18-Sep-2021 String representation (format with different directives)... Sep. 18, 2021 was a Saturday
- Related Articles
- Python Pandas - Return the Timestamp representation of the Period object
- Python Pandas - Format the string representation of the PeriodIndex object
- Python Pandas - Format the Period object and display Quarter
- 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 - Return the string alias of the Time series frequency applied on the given Period object
- 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 - Return the frequency object as a string from the PeriodIndex object
- Python Pandas - Return the nanoseconds from Timedelta object using string input
- Python Pandas - Return the microseconds from Timedelta object using string input
- Python Pandas - Get the year from the Period object
- Return the string representation of a scalar dtype in Python

Advertisements