Python Pandas - 创建一个 DateOffset 并增加日期
要创建DateOffset,请使用DateOffset()Pandas中的方法。将Increment值设置为参数。
首先,导入所需的库-
from pandas.tseries.offsets import DateOffset import pandas as pd
在Pandas中设置时间戳对象-
timestamp = pd.Timestamp('2021-09-11 02:30:55')DateOffset用于日期增量。我们在这里使用“months”参数增加月份-
print("DateOffset...\n",timestamp + DateOffset(months=2))示例
以下是代码-
from pandas.tseries.offsets import DateOffset
import pandas as pd
#在Pandas中设置时间戳对象
timestamp = pd.Timestamp('2021-09-11 02:30:55')
#显示时间戳
print("Timestamp...\n",timestamp)
#日期增量的DateOffset
# We are incrementing the months here using the "months" parameter
print("DateOffset...\n",timestamp + DateOffset(months=2))输出结果这将产生以下代码-
Timestamp... 2021-09-11 02:30:55 DateOffset... 2021-11-11 02:30:55