Python Pandas - 查詢給定 Period 物件的開始時間


若要查詢給定 Period 物件的開始時間,請使用 **period.start_time 屬性**。首先,匯入所需的庫 −

import pandas as pd

pandas.Period 表示一時間段。建立兩個 Period 物件

period1 = pd.Period("2020-09-22")
period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2, minute = 35)

顯示 Period 物件

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

從兩個 Period 物件獲取開始時間

res1 = period1.start_time
res2 = period2.start_time

示例

以下是程式碼

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2020-09-22")
period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2, minute = 35)

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

# get the start time from two Period objects
res1 = period1.start_time
res2 = period2.start_time

# Return the start time from two Period objects
print("\nDisplay the start time from the 1st Period object ...\n", res1)
print("\nDisplay the start time from the 2nd Period object ...\n", res2)

輸出

這將生成以下程式碼

Period1...
2020-09-22
Period2...
2021-02-14

Display the start time from the 1st Period object ...
2020-09-22 00:00:00

Display the start time from the 2nd Period object ...
2021-02-14 00:00:00

更新於: 2021 年 10 月 20 日

292 次瀏覽

開啟你的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.