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

Time:11-19

Using the df below, I am trying to determine a new date using dateutil.relativedelta.relativedelta but would like to iterate through df['freq'] and df['time_int'] as the **kwargs and *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

Below, I've tried creating a dict to pass through **kwargs, but the dict only contains the last values for each key. I need it to iterate through all key, value combinations.

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

desired output:

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

CodePudding user response:

pandas already contains relativedelta-like objects, pandas.DateOffset

You can use 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)

Output:

>>> 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
  • Related