Python Pandas Period.strftime
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。
Pandas Period.strftime()函数根据选择的格式,返回Period的字符串表示。 format必须是一个包含一个或几个指令的字符串。该方法识别的指令与标准Python发布的time.strftime()函数相同,以及特定的附加指令%f、%F、%q。 (格式和文档最初来自scikits.timeries)
语法 :
Period.strftime()
参数:格式
返回:字符串
示例#1:使用Period.strftime()函数以指定的格式返回给定周期的值。
# importing pandas as pd
import pandas as pd
# Create the Period object
prd = pd.Period(freq ='S', year = 2000, month = 2, day = 22,
hour = 8, minute = 21, second = 24)
# Print the Period object
print(prd)
输出 :

现在我们将使用Period.strftime()函数以(‘%b. %d, %Y是%A’)格式返回给定的周期。
注意 : ‘%b’是月份名称,%d是月份的一天,%Y是年份,%A是工作日名称。
# return the period in specified format.
prd.strftime('% b. % d, % Y was a % A')
输出 :

正如我们在输出中看到的,Period.strftime()函数以指定的格式返回了给定周期的值。
示例#2:使用Period.strftime()函数以指定的格式返回给定周期的值。
# importing pandas as pd
import pandas as pd
# Create the Period object
prd = pd.Period(freq ='S', year = 2006, month = 10,
hour = 15, minute = 49, second = 17)
# Print the object
print(prd)
输出 :

现在我们将使用Period.strftime()函数,以(‘%b-%Y’)格式返回给定的周期。
注意: ‘%b’是月份名称,%Y是年份
# return the period in specified format.
prd.strftime('% b-% Y')
输出 :

正如我们在输出中看到的,Period.strftime()函数以指定的格式返回了给定周期的值。
极客教程