Python Pandas – 检查BusinessDay偏移量是否已归一化

Python Pandas – 检查BusinessDay偏移量是否已归一化

要检查BusinessDay偏移量是否已归一化,请在Pandas中使用BusinessDay.normalize属性。

首先,导入所需的库,如下所示 –

import datetime
import pandas as pd

在Pandas中设置时间戳对象,如下所示-

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

创建BusinessDay偏移量。BusinessDay是DateOffset子类。我们使用“normalize”参数对Busin essDay进行了归一化,如下所示-

bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10), normalize=True)

显示已更新的时间戳,如下所示 –

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

检查BusinessDay偏移量是否已归一化,如下所示 –

print("\nThe BusinessDay Offset is normalized..\n", bdOffset.normalize)

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

示例

以下是代码 –

import datetime
import pandas as pd

# 在pandas中设置时间戳对象
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')

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

# 创建BusinessDay偏移量
# BusinessDay是DateOffset子类
#我们使用“normalize”参数对BusinessDay进行了归一化
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10), normalize=True)

# 显示BusinessDay偏移量
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 name of the frequency on the BusinessDay object..\n", bdOffset.name)

# 检查BusinessDay偏移量是否已归一化
print("\nThe BusinessDay Offset is normalized..\n", bdOffset.normalize)

输出

这将生成以下代码-

Timestamp...
2021-10-30 01:55:02.000045

BusinessDay Offset...
<BusinessDay: offset=datetime.timedelta(seconds=29400)>

Updated Timestamp...
2021-11-01 00:00:00

Frequency on the given BusinessDay Offset...
B+8H10Min

The name of the frequency on the BusinessDay object..
B

The BusinessDay Offset is normalized..
True

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程