Python Pandas – 返回应用于给定的 CustomBusinessHour 对象的规则代码
要返回应用于给定 CustomBusinessHour 对象的规则代码,请在 Pandas 中使用 CustomBusinessHour.rule_code 属性。
首先,导入所需的库−
import pandas as pd
在 Pandas 中设置时间戳对象−
timestamp = pd.Timestamp('2021-11-14 05:20:30')
创建 CustomBusinessHour 偏移量,CustomBusinessHour 是 DateOffset 子类−
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 4, weekmask = 'Mon Tue Wed Fri Sat' ,normalize=True)
将偏移量添加到时间戳并显示更新后的时间戳−
print("\n更新后的时间戳...\n",timestamp + cbhOffset)
返回应用于给定 CustomBusinessHour 偏移量的频率的规则代码−
print("\nCustomBusinessHour 对象的规则代码..\n", cbhOffset.rule_code)
示例
下面是代码−
import pandas as pd
# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# 显示时间戳
print("时间戳...\n",timestamp)
# 创建 CustomBusinessHour 偏移量
# CustomBusinessHour 是 DateOffset 子类
# 有效工作日的 Weekmask
# 我们使用“normalize”参数规范化了 CustomBusinessDay
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 4, weekmask = 'Mon Tue Wed Fri Sat' ,normalize=True)
# 显示 CustomBusinessHour 偏移量
print("\nCustomBusinessHour 偏移量...\n",cbhOffset)
# 将偏移量添加到时间戳并显示更新后的时间戳
print("\n更新后的时间戳...\n",timestamp + cbhOffset)
# 将应用于给定 CustomBusinessHour 偏移量的频率作为字符串返回
print("\n应用于给定 CustomBusinessHour 偏移量的频率...\n",cbhOffset.freqstr)
# 返回应用于给定 CustomBusinessHour 偏移量的规则代码
print("\nCustomBusinessHour 对象的规则代码..\n", cbhOffset.rule_code)
输出
这将生成以下代码−
时间戳...
2021-11-14 05:20:30
CustomBusinessHour 偏移量...
<4 * CustomBusinessHours: CBH=09:00-17:00>
更新后的时间戳...
2021-11-15 00:00:00
应用于给定 CustomBusinessHour 偏移量的频率...
4CBH
CustomBusinessHour 对象的规则代码..
CBH
极客教程