使用 pandas dateoffset 并向前迈进 num_years (由列给出)

Using pandas dateoffset and step forward num_years (given by a column)

我有一个包含日期时间列“Date_Begin”的数据框。我还有另一列“Years_to_add”。我想计算第三列,其中“DateBegin”向前推进了“years_to_add”列中的年数。我想避免在一年内使用 365.25 之类的近似值。我想简单地添加日历意义上的年数(我知道将一年添加到 2 月 29 日会有问题)。

在我的 pandas 版本中,我不能使用年份作为 timedelta(不再支持),所以我使用 DateOffset 方法。

我的语法不对。根据 here 的建议,我尝试了以下方法:

df["begin_date"] + pd.DateOffset(years=int(df["years_to_add"]))

df["begin_date"] + df["years_to_add"].apply(pd.DateOffset.years)

只需将其作为 lambda 函数传递

df["begin_date"] + df["years_to_add"].apply(lambda y: pd.DateOffset(years=y))