Python Pandas - 返回对业务日偏移应用的增加计数


要返回对 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("\nUpdated Timestamp...\n",timestamp + bdOffset)

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

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

示例

以下是代码 −

import datetime
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the BusinessDay Offset
# BusinessDay is the DateOffset subclass
bdOffset = pd.tseries.offsets.BusinessDay(n = 6)

# Display the BusinessDay Offset
print("\nBusinessDay Offset...\n",bdOffset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + bdOffset)

# return the frequency applied on the given BusinessDay object as a string
print("\nFrequency on the given BusinessDay Offset...\n",bdOffset.freqstr)

# return the count of increments on the given BusinessDay object
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

更新于:21-Oct-2021

82 次浏览

启动您的 事业

通过完成本课程获得认证

开始学习
广告
© . All rights reserved.