Python Pandas - 建立 DateOffset 并增量日期


使用 Pandas 中的 DateOffset() 函数来创建 DateOffset。将增量值设置为一个参数。

首先,导入所需的库 −

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

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-11 02:30:55')

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

# DateOffset for date increment
# 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

更新于: 21-10-2021

1 千次观看

开启 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.