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
广告