Python Pandas - 返回在给定的 DateOffset 对象上应用的规则代码


若要返回在给定的 DateOffset 对象上应用的规则代码,请在 Pandas 中使用 offset.rule_code。首先,导入所需的库 −

from pandas.tseries.frequencies import to_offset
import pandas as pd

在 Pandas 中设置时间戳对象 −

timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

创建 DateOffset。我们在此使用 "M" 频率增加月份 −

offset = to_offset("3M")

显示更新的时间戳 −

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

返回在给定的 DateOffset 对象上应用的频率的规则代码 −

print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

示例

以下为代码 −

from pandas.tseries.frequencies import to_offset
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the DateOffset
# We are incrementing the months here using the "M" frequency
offset = to_offset("3M")

# Display the DateOffset
print("\nDateOffset...\n",offset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)

# return the rule code of the frequency applied on the given DateOffset object
print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

输出

这将生成以下代码 −

Timestamp...
 2021-09-26 03:25:02.000045

DateOffset...
 <3 * MonthEnds>

Updated Timestamp...
 2021-11-30 03:25:02.000045

The rule code of the DateOffset object..
 M

更新于: 21-10-2021

84 浏览次数

开始您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.