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 次瀏覽

開啟您的 事業

完成課程以獲得認證

開始
廣告
© . All rights reserved.