Search code examples
pythonpandasdatediff

Pandas - Number of Months Between Two Dates


I think this should be simple but what I've seen are techniques that involve iterating over a dataframe date fields to determine the diff between two dates. And I'm having trouble with it. I'm familiar with MSSQL DATEDIFF so I thought Pandas datetime would have something similar. I perhaps it does but I'm missing it.

Is there a Pandonic way of determing the number of months as an integer between two dates (datetime) without the need to iterate? Keep in mind that there potentially are millions of rows so performance is a consideration.

The dates are datetime objects and the result would like this - new column being Month:

Date1           Date2         Months
2016-04-07      2017-02-01    11
2017-02-01      2017-03-05    1

Solution

  • Here is a very simple answer my friend:

    df['nb_months'] = (df.date2 - df.date1) / np.timedelta64(1, 'M')
    

    and now:

    df['nb_months'] = df['nb_months'].astype(int)