My dataframe has two columns. When I subtract them to get the month in between, I got some weird numbers. Here is an example:
test = pd.DataFrame({'reg_date': [datetime(2017,3,1), datetime(2016,9,1)],
'leave_date':[datetime(2017,7,1), datetime(2017,6,1)]})
test['diff_month'] = test.leave_date.dt.month - test.reg_date.dt.month
test
The output:
If a user's register_date is last year, I get a negative number (also incorrect as well).
What operations should I perform to get the correct time difference in month between two datetime column?
Update: I changed the example a bit so it reflects more about the issue I am facing. Don't down vote so fast guys.
A hack I did to fix this is:
test['real_diff'] = test.diff_month.apply(lambda x: x if x > 0 else 12+x)
I don't like the hack so I am curious if there is any other way of doing it.