Python Pandas - 格式化 Period 对象并显示没有世纪的年份

要格式化 Period 对象,请使用方法并显示没有世纪的年份,将参数设置为 %y。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)

显示结果。在这里,年份显示没有世纪

print("\nString representation (year without century)...\n", period.strftime('%y'))

示例

以下是代码

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)

# 显示结果
# 在这里,年份显示没有世纪
print("\nString representation (year without century)...\n", period.strftime('%y'))
输出结果

这将产生以下代码

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

String representation (year without century)...
21

猜你喜欢