Search code examples
pandasdatetimeincrementstrptime

How can I increment a date in datetime64[ns] format by 6 calendar months?


I never realised that incrementing a simple date in Python would be such an insurmountable challenge, but have given up after 2 hours of trying and searching on this forum. I have a dataframe with a column effective_date, which contains entries like 2019-01-02 and datatype datetime64[ns].

I've tried:

data['effective_date'] = pd.to_datetime(data['effective_date'].values)
data['six_mth_interval'] = data['effective_date'].apply(lambda x: x['effective_date'].values + relativedelta(months=6))

... but I get the following error:

<ipython-input-315-b81c59eb6b0d> in <lambda>(x)
----> 1 data['six_mth_interval'] = data['effective_date'].apply(lambda x: x['effective_date'] + relativedelta(months=6))

TypeError: 'Timestamp' object is not subscriptable

Existing articles on S/O have not been helpful.


Solution

  • Run:

    data['six_mth_interval'] = data.effective_date + pd.DateOffset(months=6)
    

    For an example DataFrame the result is:

       effective_date six_mth_interval
    0      2019-08-01       2020-02-01
    1      2019-08-02       2020-02-02
    2      2019-08-25       2020-02-25
    3      2019-08-26       2020-02-26
    4      2019-08-27       2020-02-27
    5      2019-08-28       2020-02-28
    6      2019-08-29       2020-02-29
    7      2019-08-30       2020-02-29
    8      2019-08-31       2020-02-29
    9      2019-09-01       2020-03-01
    10     2019-09-02       2020-03-02
    

    Your code failed because when you apply a function to a Series (a column of a DataFrame), then the argument is each single element of this Series.

    So you could write:

    data['six_mth_interval'] = data['effective_date'].apply(
        lambda x: x + pd.DateOffset(months=6))
    

    but my code is faster.