Python Pandas BusinessHour偏移对象 – 移动到下一个工作日
使用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("\nThe next business day...\n",timestamp + bhOffset.next_bday)
更多Pandas文章,请阅读:Pandas教程
示例
以下是代码−
import datetime
import pandas as pd
# 设置Pandas中的时间戳对象
timestamp = pd.Timestamp('2021-9-30 06:50:20')
# 显示时间戳
print("Timestamp...\n",timestamp)
# 创建BusinessHour偏移
# BusinessHour是DateOffset子类
bhOffset = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(days = 3, hours = 3))
# 显示BusinessHour偏移
print("\nBusinessHour Offset...\n",bhOffset)
# 显示下一个工作日
print("\nThe next business day...\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>
The next business day...
2021-10-01 06:50:20