Python Pandas – 返回应用于CustomBusinessHour偏移量的增量计数

Python Pandas – 返回应用于CustomBusinessHour偏移量的增量计数

要返回应用于CustomBusinessHour偏移量的增量计数,请在Pandas中使用CustomBusinessHour.n属性。

首先,导入所需的库−

import pandas as pd

在Pandas中设置时间戳对象−

timestamp = pd.Timestamp('2021-11-14 05:20:30')

创建CustomBusinessHour偏移量。 CustomBusinessHour是DateOffset子类−

cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = 'Mon Tue Wed Fri')

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

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

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

print("\n给定CustomBusinessHour对象上的增量计数...\n", cbhOffset.n)

示例

以下是代码−

import pandas as pd

# 在Pandas中设置时间戳对象
timestamp = pd.Timestamp('2021-11-14 05:20:30')

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

# 创建CustomBusinessHour偏移量
# CustomBusinessHour是DateOffset子类
# 有效工作日的Weekmask
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = 'Mon Tue Wed Fri')

# 显示CustomBusinessHour偏移量
print("\nCustomBusinessHour偏移量...\n",cbhOffset)

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

# 作为字符串返回应用于给定CustomBusinessHour偏移量对象的频率
print("\n应用于指定CustomBusinessHour偏移量对象的频率...\n",cbhOffset.freqstr)

# 返回给定CustomBusinessHour对象上的增量计数
print("\n给定CustomBusinessHour对象上的增量计数...\n", cbhOffset.n)

输出

这将生成以下代码−

时间戳...
 2021-11-14 05:20:30

CustomBusinessHour偏移量...
 <7 * CustomBusinessHours: CBH=09:00-17:00>

更新的时间戳...
 2021-11-15 16:00:00

应用于指定CustomBusinessHour偏移量对象的频率...
 7CBH

给定CustomBusinessHour对象上的增量计数...
 7

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程