Python Pandas - 格式化周期对象并在不使用世纪的情况下显示年份
要格式化周期对象,请使用 period.strftime() 方法,并且要在不使用世纪的情况下显示年份,请将参数设置为 %y。
首先,导入必要的库 −
import pandas as pd
pandas.Period 表示一段时间。创建 Period 对象
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)
显示周期对象
print("Period...\n", period)
显示结果。此处,年份以不使用世纪的方式显示
print("\nString representation (year without century)...\n", period.strftime('%y'))
示例
以下是代码
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, year is displayed without century print("\nString representation (year without century)...\n", period.strftime('%y'))
输出
这将生成以下代码
Period... 2021-09-18 08:20:45 String representation (year without century)... 21
广告