Python Pandas - 返回在 BusinessDay 偏移量上应用的增量计数
pythonserver side programmingprogramming更新于 2026/1/28 8:12:17
要返回在 BusinessDay 偏移量上应用的增量计数,请使用 Pandas 中的 BusinessDay.n 属性。
首先,导入所需的库 −
import datetime import pandas as pd
在 Pandas 中设置时间戳对象” −
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')
创建 BusinessDay 偏移量。 BusinessDay 是 DateOffset 子类 −
bdOffset = pd.tseries.offsets.BusinessDay(n = 6)
显示更新的时间戳 −
print("\n更新时间戳...\n",timestamp + bdOffset)
返回给定 BusinessDay 对象上的增量计数 −
print("\nBusinessDay 对象上的增量计数..\n", bdOffset.n)
示例
以下是代码−
import datetime
import pandas as pd
# 设置 Pandas 中的时间戳对象
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')
# 显示时间戳
print("时间戳...\n",timestamp)
# 创建 BusinessDay 偏移量
# BusinessDay 是 DateOffset 子类
bdOffset = pd.tseries.offsets.BusinessDay(n = 6)
# 显示 BusinessDay 偏移量
print("\nBusinessDay 偏移量...\n",bdOffset)
# 显示更新后的时间戳
print("\n更新时间戳...\n",timestamp + bdOffset)
# return以字符串形式将频率应用于给定的 BusinessDay 对象
print("\n给定 BusinessDay 偏移量的频率...\n",bdOffset.freqstr)
# 返回给定 BusinessDay 对象的增量计数
print("\nBusinessDay 对象的增量计数...\n", bdOffset.n)
输出
这将产生以下代码 −
时间戳... 2021-10-30 01:55:02.000045 BusinessDay 偏移量... <6 * BusinessDays> 更新时间戳... 2021-11-08 01:55:02.000045 给定 BusinessDay 偏移量的频率... 6B BusinessDay 对象的增量计数... 6
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

