首页 文章资讯内容详情

Python Pandas - 返回适用于工作日偏移量的增量计数

2026-06-03 1 花语

要返回适用于BusinessDay偏移量的增量计数,请使用BusinessDay.nPandas中的属性。

首先,导入所需的库-

import datetime import pandas as pd

在Pandas中设置时间戳对象”-

timestamp = pd.Timestamp(2021-10-30 01:55:02.000045)

创建工作日偏移量。BusinessDay是DateOffset子类-

bdOffset = pd.tseries.offsets.BusinessDay(n = 6)

显示更新的时间戳-

print("\nUpdated Timestamp...\n",timestamp + bdOffset)

返回给定BusinessDay对象的增量计数-

print("\nThe count of increments on the BusinessDay object..\n", bdOffset.n)

示例

以下是代码-

import datetime import pandas as pd #在Pandas中设置时间戳对象 timestamp = pd.Timestamp(2021-10-30 01:55:02.000045) #显示时间戳 print("Timestamp...\n",timestamp) #创建工作日抵消 #BusinessDay是DateOffset子类 bdOffset = pd.tseries.offsets.BusinessDay(n = 6) #显示工作日偏移 print("\nBusinessDay Offset...\n",bdOffset) #显示更新的时间戳 print("\nUpdated Timestamp...\n",timestamp + bdOffset) #以字符串形式返回应用于给定BusinessDay对象的频率 print("\nFrequency on the given BusinessDay Offset...\n",bdOffset.freqstr) #返回给定BusinessDay对象的增量计数 print("\nThe count of increments on the BusinessDay object..\n", bdOffset.n)输出结果

这将产生以下代码-

Timestamp... 2021-10-30 01:55:02.000045 BusinessDay Offset... <6 * BusinessDays> Updated Timestamp... 2021-11-08 01:55:02.000045 Frequency on the given BusinessDay Offset... 6B The count of increments on the BusinessDay object.. 6