The first thing that comes to my mind...
>>> from datetime import datetime, timedelta
>>> dt1 = datetime(year=2020, month=3, day=1)
>>> dt2 = datetime(year=2020, month=5, day=1)
>>> # delta = dt2-dt1
>>> delta = abs(dt2-dt1)
>>> delta
datetime.timedelta(61)
>>> delta.days
61
UPDATE:
What I meant to represent is the idea of using the absolute value of the delta -> abs()
In Python 3.10 it works with the dateutil.realtivedelta()
too
from datetime import datetime
from dateutil.relativedelta import relativedelta
city_clean_dates = [
{'signup_date': '2019-12-01', 'last_trip_date': '2020-02-01'},
{'signup_date': '2021-01-01', 'last_trip_date': '2020-05-01'},
{'signup_date': '2020-03-01', 'last_trip_date': '2020-05-31'},
]
for city_clean in city_clean_dates:
city_clean['last_trip_date'] = datetime.strptime(city_clean['last_trip_date'], '%Y-%m-%d')
city_clean['signup_date'] = datetime.strptime(city_clean['signup_date'], '%Y-%m-%d')
rd1 = abs(relativedelta(city_clean['last_trip_date'], city_clean['signup_date']))
rd2 = abs(relativedelta(city_clean['signup_date'], city_clean['last_trip_date']))
assert rd1 == rd2
print(f"Recent - old date: {rd1}")
print(f"Old - recent date: {rd2}")
this would print
Recent - old date: relativedelta(months=+2)
Old - recent date: relativedelta(months=+2)
Recent - old date: relativedelta(months=+8)
Old - recent date: relativedelta(months=+8)
Recent - old date: relativedelta(months=+2, days=+30)
Old - recent date: relativedelta(months=+2, days=+30)
Note neither of my solutions returns months, while the first one returns days only, and the second returns whole months + the extra days of the partial month.
The ambiguity of this is very obvious in the case of
{'last_trip_date': '2020-03-01', 'signup_date': '2020-05-31'}
Where normally we could say that's 3 months but in reality, it's one day short.
It's up to the developer to overcome the ambiguity of such values considering the use-case.