Python Pandas - 检查给定的 CustomBusinessHour 是否已锚定
pythonserver side programmingprogramming更新于 2026/1/29 8:44:17
要检查给定的 CustomBusinessHour 是否已锚定,请使用 Pandas 中的 CustomBusinessHour.is_anchored() 方法。
首先,导入所需的库 −
import pandas as pd
在 Pandas 中设置时间戳对象 −
timestamp = pd.Timestamp('2021-11-14 05:20:30')
创建 CustomBusinessHour 偏移量。 CustomBusinessHour 是 DateOffset 的子类 −
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")
将偏移量添加到时间戳并显示更新的时间戳 −
print("\n更新时间戳...\n",timestamp + cbhOffset)
检查 CustomBusinessHour 是否已锚定 −
print("\n检查 CustomBusinessHour 是否已锚定...\n", cbhOffset.is_anchored())
示例
以下是代码 −
import pandas as pd
# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# 显示时间戳
print("Timestamp...\n",timestamp)
# 创建 CustomBusinessHour 偏移量
# CustomBusinessHour 是 DateOffset 子类
# 此处,"start" 是 24 小时格式的自定义营业时间的开始时间。
# "end" 是 24 小时格式的自定义营业时间的结束时间。
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")
# 显示 CustomBusinessHour 偏移量
print("\nCustomBusinessHour Offset...\n",cbhOffset)
# 将偏移量添加到时间戳并显示更新的时间戳
print("\n更新时间戳...\n",timestamp + cbhOffset)
# 检查 CustomBusinessHour 是否已锚定
print("\n检查 CustomBusinessHour 是否已锚定...\n", cbhOffset.is_anchored())
输出
将产生以下代码 −
时间戳... 2021-11-14 05:20:30 CustomBusinessHour 偏移量... <CustomBusinessHour: CBH=09:30-18:30> 更新时间戳... 2021-11-15 10:30:00 检查 CustomBusinessHour 是否已锚定... True
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/
打印
下一节:Python Pandas CustomBusinessHour - 检查给定的时间戳是否在偏移量上 ❯❮ 上一节:Python Pandas - 从 CustomBusinessHour 偏移对象以 24 小时格式显示自定义营业时间的结束时间

