Python Pandas - 格式化 Period 对象并以 24 小时格式显示时间

要格式化 Period 对象,请使用方法并以 24 小时格式显示时间,将参数设置为 %H。period.strftime()

首先,导入所需的库 -

import pandas as pd

的pandas.Period代表的一段时间。创建 Period 对象

period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 17, minute = 20, second = 45)

显示 Period 对象

print("Period...\n", period)

显示结果。在这里,时间以 24 小时格式显示,即 [00,23]

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

示例

以下是代码

import pandas as pd

# Thepandas.Periodrepresents 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'))
输出结果

这将产生以下代码

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

String representation (24-Hour format)...
17

猜你喜欢