Python Pandas - 获取 Period 的第二个组成部分
pythonpandasserver side programmingprogramming
要获取 Period 的第二个组成部分,请使用 period.second 属性。首先,导入所需的库 −
import pandas as pd
pandas.Period 表示一段时间。创建两个 Period 对象
period1 = pd.Period("2020-09-23 05:55:30") period2 = pd.Period(freq="S", year = 2021, month = 7, day = 16, hour = 2,minute = 35,second = 10)
显示 Period 对象
print("Period1...\n", period1) print("Period2...\n", period2)
从两个 Period 对象中获取秒数
res1 = period1.second res2 = period2.second
示例
以下是代码
import pandas as pd # pandas.Period 表示一段时间 # 创建两个 Period 对象 period1 = pd.Period("2020-09-23 05:55:30") period2 = pd.Period(freq="S", year = 2021, month = 7, day = 16, hour = 2,minute = 35,second = 10) # 显示 Period 对象 print("Period1...\n", period1) print("Period2...\n", period2) # 从两个 Period 对象中获取秒数 res1 = period1.second res2 = period2.second # 从两个 Period 对象中返回秒数 print("\nSeconds component from the 1st Period object ...\n", res1) print("\nSeconds component from the 1st Period object ...\n", res2)
输出
将生成以下代码
Period1... 2020-09-23 05:55:30 Period2... 2021-07-16 02:35:10 Seconds component from the 1st Period object ... 30 Seconds component from the 1st Period object ... 10