Python Pandas - 返回给定 BusinessDay 偏移对象上应用的频率名称


要返回给定 BusinessDay 偏移对象上应用的频率名称,请在 Pandas 中使用 BusinessDay.name 属性。

首先,导入所需的库 -

import datetime
import pandas as pd

在 Pandas 中设置时间戳对象 -

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

创建 BusinessDay 偏移量。BusinessDay 是 DateOffset 子类 -

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

显示更新的时间戳 -

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

返回给定 BusinessDay 对象上应用的频率名称 -

print("\nThe name of the frequency on the BusinessDay object..\n", bdOffset.name)

示例

以下是代码 -

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
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10))

# 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)

输出

这将生成以下代码 -

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

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

Updated Timestamp...
 2021-11-01 10:05:02.000045

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

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

更新于: 2021-10-21

71 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.