Python Pandas – 检查是否已将CustomBusinessHour Offset归一化
要检查是否已将CustomBusinessHour Offset归一化,请在Pandas中使用CustomBusinessHour.normalize属性。
首先,导入所需的库 −
import pandas as pd
在Pandas中设置时间戳对象 −
timestamp = pd.Timestamp('2021-10-25 08:35:10')
创建CustomBusinessHour Offset。CustomBusinessHour是DateOffset子类。我们使用“normalize”参数归一化了CustomBusinessDay −
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 3, weekmask = 'Mon Tue Wed Fri Sat' ,normalize=True)
将Offset添加到Timestamp并显示更新的Timestamp −
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
检查CustomBusinessHour Offset是否已归一化 −
print("\nThe CustomBusinessHour Offset is normalized ?\n", cbhOffset.normalize)
示例
以下是代码 −
import pandas as pd
# 在Pandas中设置时间戳对象
timestamp = pd.Timestamp('2021-10-25 08:35:10')
# 显示时间戳
print("Timestamp...\n",timestamp)
# 创建CustomBusinessHour Offset
# CustomBusinessHour是DateOffset子类
# 有效工作日的Weekmask
# 我们使用“normalize”参数归一化了CustomBusinessDay
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 3, weekmask = 'Mon Tue Wed Fri Sat',normalize=True)
# 显示CustomBusinessHour Offset
print("\nCustomBusinessHour Offset...\n",cbhOffset)
# 将Offset添加到Timestamp并显示更新的Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
# 返回应用于给定CustomBusinessHour Offset对象的频率,作为字符串
print("\nFrequency applied on the given CustomBusinessHour Offset object...\n",cbhOffset.freqstr)
# 返回应用于给定CustomBusinessHour对象的频率的名称
print("\nThe name of the frequency on the CustomBusinessHour object..\n", cbhOffset.name)
# 检查CustomBusinessHour Offset是否已归一化
print("\nThe CustomBusinessHour Offset is normalized ?\n", cbhOffset.normalize)
输出
这将产生以下代码 −
Timestamp...
2021-10-25 08:35:10
CustomBusinessHour Offset...
<3 * CustomBusinessHours: CBH=09:00-17:00>
Updated Timestamp...
2021-10-25 00:00:00
Frequency applied on the given CustomBusinessHour Offset object...
3CBH
The name of the frequency on the CustomBusinessHour object..
CBH
The CustomBusinessHour Offset is normalized ?
True
极客教程