Home > database >  Get the average number of days between two dates in Python
Get the average number of days between two dates in Python

Time:12-06

I have a data frame with only dates in one column. My objective is to calculate the number of days between each date, in order to get the average number of days between two dates (each date corresponds to an operation).

I tried doing something like this :

for i in range(len(df)):
    if i != 0:
        d0 = df.iloc[[i-1,1]]
        d1 = df.iloc[[i,1]]
        L.append((d1 - d0).days)

But got the error message : 'unsupported operand type(s) for -: 'str' and 'str''

CodePudding user response:

You can subtract a date from another if they're in proper format. Maybe a timestamp in seconds, maybe a proper datetime object. As you might've noticed, you can't subtract strings.

If the date is in the ISO format, it's the easiest, just do this before the subtraction:

from datetime import datetime
d0 = datetime.fromisoformat(d0)
d1 = datetime.fromisoformat(d1)

The result will be in a datetime.timedelta format with a .total_seconds() method and even a .days attribute, so you get days like this:

difference_in_days = (d1 - d0).days

If it's something else than an ISO string, check out this question: Converting string into datetime

  • Related