Python Pandas - 返回在 CustomBusinessHour 偏移上应用的增量计数
pythonserver side programmingprogramming更新于 2026/1/29 6:36:17
要返回在 CustomBusinessHour 偏移上应用的增量计数,请使用 Pandas 中的 CustomBusinessHour.n 属性。
首先,导入所需的库 −
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)
返回给定 CustomBusinessHour 对象上的增量计数 −
print("\nCustomBusinessHour 对象的增量计数....\n", cbhOffset.n)
示例
以下是代码 −
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)
# 返回给定 CustomBusinessHour 对象上的增量计数
print("\nCustomBusinessHour 对象的增量计数。\n", cbhOffset.n)
输出
这将产生以下代码 −
时间戳... 2021-11-14 05:20:30 CustomBusinessHour 偏移量... <7 * CustomBusinessHours: CBH=09:00-17:00> 更新时间戳... 2021-11-15 16:00:00 应用于给定的 CustomBusinessHour Offset 对象的频率... 7CBH CustomBusinessHour 对象的增量计数.. 7
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/
打印
下一节:Python Pandas - 获取在 CustomBusinessHour 偏移量上应用的 weekmask ❯❮ 上一节:Python Pandas - 返回应用于给定 CustomBusinessHour 对象的规则代码

