Python——從 Pandas 中的時間戳物件獲取工作日
要從時間戳物件中獲取工作日,請使用timestamp.weekday()方法。首先,匯入必要的庫 -
import pandas as pd import datetime
在 Pandas 中設定時間戳。建立一個時間戳物件
timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12))
獲取今年中的工作日。工作日由數字表示,星期一 == 0,星期二 == 1……星期日 == 6
timestamp.weekday()
示例
以下是程式碼
import pandas as pd import datetime # set the timestamp in Pandas # create a Timestamp object timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) # display the Timestamp print("Timestamp...\n", timestamp) # getting the weekday of the year res = timestamp.weekday() # display only the weekday # weekday represented by number Monday == 0, Tuesday == 1 … Sunday == 6 print("\nGet the weekday from Timestamp...\n", res)
輸出
這將產生以下程式碼
Timestamp... 2021-05-12 00:00:00 Get the weekday from Timestamp... 2
廣告