Python Pandas - 获取周期中的第二个分量


若需获取周期的第二个分量,可使用**period.second** 属性。首先,导入必需的库 −

import pandas as pd

pandas.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)

显示周期对象

print("Period1...\n", period1)
print("Period2...\n", period2)

获取两个周期对象中的秒

res1 = period1.second
res2 = period2.second

例子

以下是代码

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
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)

# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

# get the seconds from two Period objects
res1 = period1.second
res2 = period2.second

# Return the seconds from the two Period objects
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

更新日期:14-10-2021

75 次浏览

开启您的 事业

完成课程以获得认证

开始
广告