通过 **kwargs 和 *args 传递两个 df 列以使用 relativedelta 创建一个新的日期列

pass two df columns through **kwargs and *args to create a new date column using relativedelta

使用下面的 df,我正在尝试使用 dateutil.relativedelta.relativedelta 确定新日期 但想遍历 df['freq'] 和 df['time_int'] 作为 **kwargs 和 *args

In [1]: df
Out[1]: 
        date  time_int    freq
0 2021-11-16        -5    days
1 2021-11-16        -1   weeks
2 2021-11-16        -2   weeks
3 2021-11-16        -3   weeks
4 2021-11-16        -3   weeks
5 2021-11-16        -1  months
6 2021-11-16        -1  months

下面,我尝试创建一个 dict 来传递 **kwargs,但该 dict 只包含每个键的最后一个值。我需要它遍历所有键值组合。

time_dict  = dict(zip(df.freq,df.time_int))
df['new_date'] = df['date'].dt.date + relativedelta(**time_dict)

期望的输出:

test
Out[170]: 
        date  time_int    freq new_date
0 2021-11-16        -5    days 2021-11-11
1 2021-11-16        -1   weeks 2021-11-09
2 2021-11-16        -2   weeks 2021-11-02
3 2021-11-16        -3   weeks 2021-10-26
4 2021-11-16        -3   weeks 2021-10-26
5 2021-11-16        -1  months 2021-10-16
6 2021-11-16        -1  months 2021-10-16

pandas 已经包含 relativedelta 类对象,pandas.DateOffset

您可以使用Series.apply

def compute_new_date(row):
    return row['date'] + pd.DateOffset(**{row['freq']: row['time_int']})

# apply compute_new_date row-wise
df['new_date'] = df.apply(compute_new_date, axis=1)

输出:

>>> df

        date  time_int    freq   new_date
0 2021-11-16        -5    days 2021-11-11
1 2021-11-16        -1   weeks 2021-11-09
2 2021-11-16        -2   weeks 2021-11-02
3 2021-11-16        -3   weeks 2021-10-26
4 2021-11-16        -3   weeks 2021-10-26
5 2021-11-16        -1  months 2021-10-16
6 2021-11-16        -1  months 2021-10-16