Python Pandas - 将给定的BusinessDay Offset对象频率作为字符串返回


要将给定的BusinessDay Offset对象的应用频率作为字符串返回,请在Pandas中使用BusinessDay.freqstr属性。

首先,导入所需的库:

import datetime
import pandas as pd

在Pandas中设置时间戳对象:

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

创建BusinessDay Offset。BusinessDay是DateOffset的子类:

bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(days = 7, hours = 7, minutes = 7))

显示更新后的时间戳:

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

将应用于给定BusinessDay对象的频率作为字符串返回:

print("\nFrequency on the given BusinessDay Offset...\n",bdOffset.freqstr)

示例

以下是代码:

import datetime
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-1-1 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(offset = datetime.timedelta(days = 7, hours = 7, minutes = 7))

# Display the BusinessDay Offset
print("BusinessDay 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)

输出

这将产生以下代码:

Timestamp...
 2021-01-01 01:55:02.000045
BusinessDay Offset...
 <BusinessDay: offset=datetime.timedelta(days=7, seconds=25620)>

Updated Timestamp...
 2021-01-11 09:02:02.000045

Frequency on the given BusinessDay Offset...
 B+7D7H7Min

更新于:2021年10月21日

67 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告