Pandas Timedelta in months

Update for pandas 1.3

If you want integers instead of MonthEnd objects:

df['elapsed_months'] = df.today.dt.to_period('M').view(dtype='int64') - df.date.dt.to_period('M').view(dtype='int64')
df

# Out[11]: 
#         date      today  elapsed_months
# 0 2016-10-11 2016-12-02               2
# 1 2016-11-01 2016-12-02               1

This works with pandas 1.1.1:

df['elapsed_months'] = df.today.dt.to_period('M').astype(int) - df.date.dt.to_period('M').astype(int)
df

# Out[11]: 
#         date      today  elapsed_months
# 0 2016-10-11 2016-12-02               2
# 1 2016-11-01 2016-12-02               1

you could also try:

df['months'] = (df['today'] - df['date']) / np.timedelta64(1, 'M')
df
#      date      today    months
#0 2016-10-11 2016-12-02  1.708454
#1 2016-11-01 2016-12-02  1.018501

Update for pandas 0.24.0:

Since 0.24.0 has changed the api to return MonthEnd object from period subtraction, you could do some manual calculation as follows to get the whole month difference:

12 * (df.today.dt.year - df.date.dt.year) + (df.today.dt.month - df.date.dt.month)

# 0    2
# 1    1
# dtype: int64

Wrap in a function:

def month_diff(a, b):
    return 12 * (a.dt.year - b.dt.year) + (a.dt.month - b.dt.month)

month_diff(df.today, df.date)
# 0    2
# 1    1
# dtype: int64

Prior to pandas 0.24.0. You can round the date to Month with to_period() and then subtract the result:

df['elapased_months'] = df.today.dt.to_period('M') - df.date.dt.to_period('M')

df
#         date       today  elapased_months
#0  2016-10-11  2016-12-02                2
#1  2016-11-01  2016-12-02                1

In a simpler way, it can also be calculated using the to_period function in pandas.

pd.to_datetime('today').to_period('M') - pd.to_datetime('2020-01-01').to_period('M')
# [Out]:
# <7 * MonthEnds>

In case, you just want the integer value just use (<above_code>).n

Tags:

Python

Pandas