Pandas Python - 格式化和返回周期对象的字符串表示形式
要格式化和返回周期对象的字符串表示形式,请使用 period.strftime() 方法。然后,将格式说明符设置为参数,如 strftime('%d-%b-%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)
显示 Period 对象
print("Period...\n", period)
显示格式化的字符串表示形式
print("\nString representation (format)...\n", period.strftime('%d-%b-%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 print("\nString representation (format)...\n", period.strftime('%d-%b-%Y')) # display the result print("\nString representation (format with different directives)...\n", period.strftime('%b. %d, %Y was a %A'))
输出
这将产生以下代码
Period... 2021-09-18 08:20:45 String representation (format)... 18-Sep-2021 String representation (format with different directives)... Sep. 18, 2021 was a Saturday
广告