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')
创建日期偏移量。我们在这里使用“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 #在Pandas中设置时间戳对象 timestamp = pd.Timestamp('2021-08-30 02:30:55') #显示时间戳 print("Timestamp...\n",timestamp) #创建日期偏移 # We are incrementing the months here using the "months" parameter offset = pd.tseries.offsets.DateOffset(months=3) #显示日期偏移 print("\nDateOffset...\n",offset) #显示更新的时间戳 print("\nUpdated Timestamp...\n",timestamp + offset) #作为字符串应用于给定DateOffset对象的频率 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>