Python Pandas - 格式化 Period 对象并显示 Quarter
要格式化Period对象,使用方法并显示Quarter,将参数设置为Q%q。period.strftime()
首先,导入所需的库-
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对象被格式化并显示Quarter
print("\nString representation (display quarter)...\n", period.strftime('Q%q'))
示例
以下是代码
import pandas as pd #Thepandas.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对象被格式化并显示Quarter print("\nString representation (display quarter)...\n", period.strftime('Q%q'))输出结果
这将产生以下代码
Period... 2021-09-18 08:20:45 String representation (display quarter)... Q3