calculate datetime-difference in years, months, etc. in a new pandas dataframe column

beta picture beta · Jul 18, 2015 · Viewed 35.4k times · Source

I have a pandas dataframe looking like this:

Name    start        end
A       2000-01-10   1970-04-29

I want to add a new column providing the difference between the start and end column in years, months, days.

So the result should look like:

Name    start        end          diff
A       2000-01-10   1970-04-29   29y9m etc.

the diff column may also be a datetime object or a timedelta object, but the key point for me is, that I can easily get the Year and Month out of it.

What I tried until now is:

df['diff'] = df['end'] - df['start']

This results in the new column containing 10848 days. However, I do not know how to convert the days to 29y9m etc.

Answer

jomesoke picture jomesoke · Jan 29, 2020

You can try by creating a new column with years in this way:

df['diff_year'] = df['diff'] / np.timedelta64(1, 'Y')