Python Pandas - 返回给定 BusinessDay 对象上应用的规则代码
要返回给定 BusinessDay 对象上应用的规则代码,请在 Pandas 中使用 BusinessDay.rule_code 属性。
首先,导入所需的库 -
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 rule code of the BusinessDay object..\n", bdOffset.rule_code)
示例
以下是代码 -
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 rule code of the frequency applied on the given BusinessDay Offset print("\nThe rule code of the BusinessDay object..\n", bdOffset.rule_code)
输出
这将生成以下代码 -
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 rule code of the BusinessDay object.. B
广告