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 # 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'))
输出
这将生成以下代码
Period... 2021-09-18 08:20:45 String representation (display quarter)... Q3
广告