Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 763
shavar Avatar asked Mar 15 '17 23:03

shavar


People also ask

How do you calculate the number of 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 the number of months between two dates in Python?

Use the relativedelta. months + relativedelta. years * 12 formula to get the total months between two dates.

How do you subtract two dates in Python?

Use the strptime(date_str, format) function to convert a date string into a datetime object as per the corresponding format . To get the difference between two dates, subtract date2 from date1.


1 Answers

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) 
like image 100
ℕʘʘḆḽḘ Avatar answered Sep 27 '22 21:09

ℕʘʘḆḽḘ