Python Pandas BusinessHour 偏移对象 - 移至下一个工作日
pythonserver side programmingprogramming更新于 2026/1/28 20:28:17
使用 Pandas 中的 BusinessHour.next_bday 属性移至下一个工作日。首先,导入所需的库 −
import datetime import pandas as pd
创建 BusinessHour 偏移。 BusinessHour 是 DateOffset 子类 −
bhOffset = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(days = 3, hours = 3))
显示 BusinessHour 偏移 −
print("\nBusinessHour Offset...\n",bhOffset)
在 Pandas 中设置时间戳对象 −
timestamp = pd.Timestamp('2021-9-30 06:50:20')
显示下一个工作日 −
print("\n下一个工作日...\n",timestamp + bhOffset.next_bday)
示例
以下是代码 −
import datetime
import pandas as pd
# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-9-30 06:50:20')
# 显示时间戳
print("时间戳...\n",timestamp)
# 创建 BusinessHour偏移量
# BusinessHour 是 DateOffset 子类
bhOffset = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(days = 3, hours = 3))
# 显示 BusinessHour 偏移量
print("\nBusinessHour Offset...\n",bhOffset)
# 显示下一个工作日
print("\n下一个工作日...\n",timestamp + bhOffset.next_bday)
输出
将产生以下代码 −
Timestamp... 2021-09-30 06:50:20 BusinessHour Offset... <BusinessHour: offset=datetime.timedelta(days=3, seconds=10800): BH=09:00-17:00> 下一个工作日... 2021-10-01 06:50:20
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

