Python Pandas – 创建CustomBusinessDay偏移量对象

Python Pandas – 创建CustomBusinessDay偏移量对象

要创建CustomBusinessDay偏移量对象,请在Pandas中使用pd.tseries.offsets.CustomBusinessDay()方法。

首先导入所需的库-

import pandas as pd

创建CustomBusinessDay偏移量。CustomBusinessDay是代表在不包括假期的情况下排除自定义工作日的DateOffset子类。有效工作日的Weekmask-

cbdOffset = pd.tseries.offsets.CustomBusinessDay(n = 5, weekmask = 'Mon Tue Wed Fri')

在Pandas中设置时间戳对象-

timestamp = pd.Timestamp('2021-12-31 08:35:10')

将偏移量添加到时间戳并显示更新的时间戳-

print("\n更新的时间戳...\n",timestamp + cbdOffset)

更多Pandas文章,请阅读:Pandas教程

示例

以下是代码-

import pandas as pd

# 在Pandas中设置时间戳对象
timestamp = pd.Timestamp('2021-12-31 08:35:10')

# 显示时间戳
print("时间戳...\n",timestamp)

# 创建CustomBusinessDay偏移量
# CustomBusinessDay是代表在不包括假期的情况下排除自定义工作日的DateOffset子类
# 有效工作日的Weekmask
cbdOffset = pd.tseries.offsets.CustomBusinessDay(n = 5, weekmask = 'Mon Tue Wed Fri')

# 显示CustomBusinessDay偏移量
print("\nCustomBusinessDay偏移量...\n",cbdOffset)

# 将偏移量添加到时间戳并显示更新的时间戳
print("\n更新的时间戳...\n",timestamp + cbdOffset)

输出

这将产生以下代码-

时间戳...
 2021-12-31 08:35:10

CustomBusinessDay偏移量...
 <5 * CustomBusinessDays>

更新的时间戳...
 2022-01-10 08:35:10

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程