Python Pandas - 格式化并返回 Period 对象的字符串表示形式
要格式化并返回Period对象的字符串表示形式,请使用方法。这样,将格式说明符设置为strftime('%d-%b-%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 (format)...\n", period.strftime('%d-%b-%Y'))示例
以下是代码
import pandas as pd
#Thepandas.Periodrepresentsaperiodoftime
#CreatingaPeriodobject
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)
#displaythePeriodobject
print("Period...\n", period)
#displaytheresult
print("\nString representation (format)...\n", period.strftime('%d-%b-%Y'))
#displaytheresult
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
热门推荐
10 冬至吃汤圆祝福语简短
11 饭店开门大吉祝福语简短
12 回复小孩生日祝福语简短
13 新年的祝福语朋友简短
14 团圆日祝福语简短
15 舞蹈培训开业祝福语简短
16 婚礼闺蜜祝福语简短
17 理财公司新春简短祝福语
18 最新中秋祝福语简短霸气