Python Pandas - 获取时段所在星期几


要获取时段所在星期,请使用 period.dayofweek 属性

首先,导入必需的库 -

import pandas as pd

pandas.Period 表示一段时间。创建两个 Period 对象 -

period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)

显示 Period 对象 -

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

从两个 Period 对象中获取星期几 -

res1 = period1.dayofweek
res2 = period2.dayofweek

示例

以下为代码 -

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2021-09-18")
period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)

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

# get the day of week from two Period objects
res1 = period1.dayofweek
res2 = period2.dayofweek

# Return the day of the week from the two Period objects
# Results shows Monday=0, Tuesday=1, ... ,Sunday=6
print("\nDay of the week from the 1st Period object ...\n", res1)
print("\nDay of the week from the 2nd Period object...\n", res2)

输出

这将产生以下代码 -

Period1...
2021-09-18
Period2...
2021-09-22

更新日期: 14-10-2021

107 次查看

开启你的 职业生涯

通过完成课程获取认证

开始
广告
© . All rights reserved.