Python Pandas CustomBusinessHour – 检查给定时间戳是否偏移

Python Pandas CustomBusinessHour – 检查给定时间戳是否偏移

使用Pandas的CustomBusinessHour.is_on_offset()方法检查给定时间戳是否偏移。将时间戳作为参数传递以进行检查。

首先, 导入所需的库−

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)

检查给定时间戳是否偏移−

offset = cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30'))

显示结果−

print("\n检查给定时间戳是否偏移...\n",offset)

例子

以下是代码−

import pandas as pd

# 在Pandas中设置时间戳对象
timestamp = pd.Timestamp('2021-11-14 05:20:30')

# 显示时间戳
print("时间戳...\n",timestamp)
# 创建CustomBusinessHour偏移
# CustomBusinessHour是DateOffset子类
# 这里,“start”是您自定义工作时间的开始时间,以24小时格式显示。
# “end”是您自定义的业务时间的结束时间,在24小时格式中显示。
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")

# 显示CustomBusinessHour偏移
print("\nCustomBusinessHour偏移量...\n",cbhOffset)

# 将偏移添加到时间戳并显示更新后的时间戳
print("\n更新后的时间戳...\n",timestamp + cbhOffset)

# 检查给定时间戳是否偏移
offset = cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30'))

# 显示结果
print("\n检查给定时间戳是否是偏移...\n",offset)

输出结果

这将生成以下代码−

时间戳...
 2021-11-14 05:20:30

CustomBusinessHour偏移量...
 <CustomBusinessHour: CBH=09:30-18:30>

更新后的时间戳...
 2021-11-15 10:30:00

检查给定时间戳是否是偏移...
 False

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程