Python Pandas - 返回在给定的 CustomBusinessHour 偏移对象上应用的频率名称
pythonserver side programmingprogramming更新于 2026/1/29 5:00:17
要返回在给定的 CustomBusinessHour 偏移对象上应用的频率名称,请使用 Pandas 中的 CustomBusinessHour.name 属性。
首先,导入所需的库 −
import pandas as pd
在 Pandas 中设置时间戳对象 −
timestamp = pd.Timestamp('2021-10-25 08:35:10')
创建 CustomBusinessHour 偏移。 CustomBusinessHour 是 DateOffset 子类 −
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 3, weekmask = 'Mon Tue Wed Fri Sat')
将偏移量添加到时间戳并显示更新的时间戳 −
print("\n更新时间戳...\n",timestamp + cbhOffset)
返回应用于给定 CustomBusinessHour 对象的频率名称 −
print("\nCustomBusinessHour 上的频率名称object..\n", cbhOffset.name)
示例
以下是代码 −
import pandas as pd
# 在 Pandas 中设置时间戳对象
timestamp = pd.Timestamp('2021-10-25 08:35:10')
# 显示时间戳
print("时间戳...\n",timestamp)
# 创建 CustomBusinessHour 偏移量
# CustomBusinessHour 是 DateOffset 子类
# 有效工作日的 Weekmask
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 3, weekmask = 'Mon Tue Wed Fri Sat')
# 显示 CustomBusinessHour偏移量
print("\nCustomBusinessHour 偏移量...\n",cbhOffset)
# 将偏移量添加到时间戳并显示更新的时间戳
print("\n更新时间戳...\n",timestamp + cbhOffset)
# 以字符串形式返回应用于给定 CustomBusinessHour Offset 对象的频率
print("\n应用于给定 CustomBusinessHour Offset 对象的频率...\n",cbhOffset.freqstr)
# 返回应用于给定 CustomBusinessHour 对象的频率的名称
print("\nCustomBusinessHour 对象上的频率名称..\n",cbhOffset.name)
输出
将产生以下代码 −
时间戳... 2021-10-25 08:35:10 CustomBusinessHour 偏移量... <3 * CustomBusinessHours: CBH=09:00-17:00> 更新时间戳... 2021-10-25 12:00:00 应用于给定 CustomBusinessHour Offset 对象的频率... 3CBH CustomBusinessHour 对象上的频率名称.. CBH
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/
打印
下一节:Python Pandas - 检查 CustomBusinessHour Offset 是否已标准化 ❯❮ 上一节:Python Pandas - 显示应用于给定 CustomBusinessHour 对象的关键字参数

