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”参数对 BusinessDay 进行了规范化 −

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)

示例

以下为代码 −

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
# We have normalized the BusinessDay using the "normalize" parameter
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10), normalize=True)

# 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 name of the frequency applied on the given BusinessDay object
print("\nThe name of the frequency on the BusinessDay object..\n", bdOffset.name)

# check whether the BusinessDay Offset is normalized or not
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

更新日期: 21-10-2021

101 次浏览

开启您的 职业

完成课程并获得认证

立即开始
广告