Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 939
beta Avatar asked Jul 18 '15 11:07

beta


People also ask

How do you calculate the difference in months between two dates in pandas?

Use df. dates1-df. dates2 to find the difference between the two dates and then convert the result in the form of months.

How do I calculate time difference between two columns in pandas?

To calculate time difference between two Python Pandas columns in hours and minutes, we can subtract the datetime objects directly. We create a Panda DataFrame with 3 columns. Then we set the values of the to and fr columns to Pandas timestamps.

How do you make a pandas time series?

Pandas has various functions to create a date series. You can use the date_range method for timestamps, the period_range method for the period, and the timedelta_range method for time delta data. The date_range method is used to get a fixed frequency DatetimeIndex.


1 Answers

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

df['diff_year'] = df['diff'] / np.timedelta64(1, 'Y') 
like image 69
jomesoke Avatar answered Sep 29 '22 18:09

jomesoke