Python Pandas – 格式化 Period 对象并显示季度

Python Pandas – 格式化 Period 对象并显示季度

要格式化 Period 对象,使用 period.strftime() 方法,要显示季度,将参数设置为 Q%q。

首先,导入所需库——

import pandas as pd

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

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

显示 Period 对象

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

显示结果。在这里,Period 对象被格式化并显示季度

print("\nString representation (display quarter)...\n", period.strftime('Q%q'))

示例

下面是代码

import pandas as pd

# pandas.Period 表示一段时间。
# 创建 Period 对象
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)

# 显示 Period 对象
print("Period...\n", period)

# 显示结果
# 在这里,Period 对象被格式化并显示季度
print("\nString representation (display quarter)...\n", period.strftime('Q%q'))

输出

这将产生以下代码

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

String representation (display quarter)...
Q3

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程