Python Pandas - 返回给定 DateOffset 对象的频率作为字符串
要返回给定 DateOffset 对象上应用的频率作为字符串,请在 Pandas 中使用 **offset.freqstr** 属性。
首先,导入所需的库 -
from pandas.tseries.offsets import DateOffset import pandas as pd
在 Pandas 中设置时间戳对象 -
timestamp = pd.Timestamp('2021-08-30 02:30:55')
创建 DateOffset。我们在这里使用“months”参数递增月份 -
offset = pd.tseries.offsets.DateOffset(months=3)
显示更新的时间戳 -
print("\nUpdated Timestamp...\n",timestamp + offset)
作为字符串返回给定 DateOffset 对象上应用的频率 -
print("\nFrequency on the given DataOffset...\n",offset.freqstr)
示例
以下是代码 -
from pandas.tseries.offsets import DateOffset import pandas as pd # Set the timestamp object in Pandas timestamp = pd.Timestamp('2021-08-30 02:30:55') # Display the Timestamp print("Timestamp...\n",timestamp) # Create the DateOffset # We are incrementing the months here using the "months" parameter offset = pd.tseries.offsets.DateOffset(months=3) # Display the DateOffset print("\nDateOffset...\n",offset) # Display the Updated Timestamp print("\nUpdated Timestamp...\n",timestamp + offset) # frequency applied on the given DateOffset object as a string print("\nFrequency on the given DataOffset...\n",offset.freqstr)
输出
这将生成以下代码 -
Timestamp... 2021-08-30 02:30:55 DateOffset... <DateOffset: months=3> Updated Timestamp... 2021-11-30 02:30:55 Frequency on the given DataOffset... <DateOffset: months=3>
广告