Python Pandas – 检查 BusinessHour Offset 是否已经规范化
若要检查 BusinessHour Offset 是否已经规范化,请在 Pandas 中使用 BusinessHour.normalize 属性。
首先,导入所需的库−
import pandas as pd
在 Pandas 中设定时间戳对象−
timestamp = pd.Timestamp('2021-1-1 01:55:30')
创建 BusinessHour Offset。我们使用 “normalize” 参数规范化 BusinessHour−
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00", normalize=True)
显示更新后的时间戳−
print("\n更新后的时间戳...\n",timestamp + bhOffset)
检查 BusinessHour Offset 是否已规范化−
print("\nBusinessHour Offset 是否已规范化?\n", bhOffset.normalize)
例子
以下是代码−
import pandas as pd
# 在 Pandas 中设定时间戳对象
timestamp = pd.Timestamp('2021-1-1 01:55:30')
# 显示时间戳
print("时间戳为...\n",timestamp)
# 创建 BusinessHour Offset
# BusinessHour 是 DateOffset 子类
# 这里,“start”是您自定义业务时间的开始时间,以24小时格式表示。
# “end”是您自定义业务时间的结束时间,以24小时格式表示。
# 我们使用 "normalize" 参数规范化 BusinessHour
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00", normalize=True)
# 显示 BusinessHour Offset
print("\nBusinessHour Offset 为...\n",bhOffset)
# 显示更新后的时间戳
print("\n更新后的时间戳为...\n",timestamp + bhOffset)
# 检查 BusinessHour Offset 是否已规范化
print("\nBusinessHour Offset 是否已规范化?\n", bhOffset.normalize)
输出
这将产生以下代码−
时间戳为...
2021-01-01 01:55:30
BusinessHour Offset 为...
<BusinessHour: BH=09:30-18:00>
更新后的时间戳为...
2021-01-01 00:00:00
BusinessHour Offset 是否已规范化?
True
极客教程