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
Use df. dates1-df. dates2 to find the difference between the two dates and then convert the result in the form of months.
Use the relativedelta. months + relativedelta. years * 12 formula to get the total months between two dates.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With