Python Pandas - Format the Period object and display the Time with 24-Hour format

PythonServer Side ProgrammingProgramming

To format the Period object, use the period.strftime() method and to display the time with 24-Hour format, set the parameter as %H.

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)

Display the result. Here, Time is displayed with 24-Hour format i.e. [00,23]

print("\nString representation (24-Hour format)...\n", period.strftime('%H'))

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)

# display the result
# Here, Time is displayed with 24-Hour format i.e. [00,23]
print("\nString representation (24-Hour format)...\n", period.strftime('%H'))

Output

This will produce the following code

Period...
2021-09-18 17:20:45

String representation (24-Hour format)...
17
raja
Published on 20-Oct-2021 06:53:27
Advertisements