Python Pandas - 获取周期所在的星期几
要获取 Period 所在的星期几,请使用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.dayofweekres2 = period2.dayofweek
示例
以下是代码 -
import pandas as pd输出结果# Thepandas.Periodrepresents 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
以上是 Python Pandas - 获取周期所在的星期几 的全部内容, 来源链接: utcz.com/z/341277.html