Python Pandas - 获取在 CustomBusinessHour 偏移量上应用的 weekmask
pythonserver side programmingprogramming更新于 2026/1/29 7:08:17
要获取在 CustomBusinessHour 偏移量上应用的 weekmask,请使用 Pandas 中的 CustomBusinessHour.weekmask 属性。
首先,导入所需的库 −
import pandas as pd
在 Pandas 中设置时间戳对象 −
timestamp = pd.Timestamp('2021-11-14 05:20:30')
创建 CustomBusinessHour 偏移量。CustomBusinessHour 是 DateOffset 子类。有效工作日的星期掩码 −
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = 'Mon Tue Wed Fri')
将偏移量添加到时间戳并显示更新的时间戳 −
print("\n更新时间戳...\n",timestamp + cbhOffset)
显示星期掩码 −
print("\nCustomBusinessHour 对象上的星期掩码...\n", cbhOffset.weekmask)
示例
以下是代码 −
import pandas as pd
# 设置 Pandas 中的时间戳对象
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# 显示时间戳
print("时间戳...\n",timestamp)
# 创建 CustomBusinessHour 偏移量
# CustomBusinessHour 是 DateOffset 子类
# 有效工作日的 Weekmask
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = 'Mon Tue Wed Fri')
# 显示 CustomBusinessHour偏移量
print("\nCustomBusinessHour 偏移量...\n",cbhOffset)
# 将偏移量添加到时间戳并显示更新的时间戳
print("\n更新时间戳...\n",timestamp + cbhOffset)
# 以字符串形式返回应用于给定 CustomBusinessHour Offset 对象的频率
print("\n应用于给定 CustomBusinessHour Offset 对象的频率...\n",cbhOffset.freqstr)
# 显示星期掩码
print("\nCustomBusinessHour 对象上的星期掩码...\n", cbhOffset.weekmask)
输出
将产生以下代码 −
时间戳... 2021-11-14 05:20:30 CustomBusinessHour 偏移量... <7 * CustomBusinessHours: CBH=09:00-17:00> 更新时间戳... 2021-11-15 16:00:00 应用于给定 CustomBusinessHour Offset 对象的频率... 7CBH CustomBusinessHour 对象上的星期掩码。 Mon Tue Wed Fri
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/
打印
下一节:Python Pandas - 从 CustomBusinessHour 偏移对象以 24 小时格式显示自定义营业时间的开始时间 ❯❮ 上一节:Python Pandas - 返回在 CustomBusinessHour 偏移上应用的增量计数

