简体   繁体   中英

calculate average of days between a list of dates

I'm trying to calculate the posting frequency of a user on Instagram. Therefore I've built a list of their most recent post dates. Like this:

['01-23-2019', '01-19-2019', '01-12-2019', '12-30-2018', '12-28-2018', '12-20-2018', '11-21-2018', '11-09-2018', '10-26-2018', '10-12-2018', '09-30-2018', '09-16-2018', '09-06-2018', '08-31-2018', '08-15-2018', '08-12-2018', '08-09-2018', '07-30-2018', '07-27-2018', '07-24-2018', '07-20-2018', '07-17-2018', '07-14-2018', '07-08-2018', '07-06-2018', '06-30-2018', '06-26-2018', '06-13-2018', '06-08-2018', '06-06-2018', '05-28-2018', '05-21-2018', '05-19-2018', '05-11-2018', '05-08-2018', '05-03-2018', '05-01-2018', '04-12-2018', '04-05-2018', '03-31-2018', '03-27-2018', '03-10-2018', '03-06-2018', '02-25-2018', '02-21-2018', '02-18-2018', '02-16-2018', '02-11-2018', '02-06-2018', '02-03-2018']

Ideally, I want to get the average amount of days between the post dates. So I end up with a frequency number: eg 'the user posts every n days'.

I'm taking the timestamp from the JSON code and converting it to something readable like this:

import datetime
#prepare timestamp to calculate frequency
taken_on = post_details['taken_at_timestamp']
readable_post_date = datetime.datetime.fromtimestamp(taken_on).strftime('%m-%d-%Y')
post_dates.append(readable_post_date)

How should I best approach this to get a decimal result?

Just sum up the difference between dates and divide by the total. To keep it simple

from datetime import datetime, timedelta

dates = ['01-23-2019', '01-19-2019', '01-12-2019', '12-30-2018', '12-28-2018', '12-20-2018', '11-21-2018', '11-09-2018', '10-26-2018', '10-12-2018', '09-30-2018', '09-16-2018', '09-06-2018', '08-31-2018', '08-15-2018', '08-12-2018', '08-09-2018', '07-30-2018', '07-27-2018', '07-24-2018', '07-20-2018', '07-17-2018', '07-14-2018', '07-08-2018', '07-06-2018', '06-30-2018', '06-26-2018', '06-13-2018', '06-08-2018', '06-06-2018', '05-28-2018', '05-21-2018', '05-19-2018', '05-11-2018', '05-08-2018', '05-03-2018', '05-01-2018', '04-12-2018', '04-05-2018', '03-31-2018', '03-27-2018', '03-10-2018', '03-06-2018', '02-25-2018', '02-21-2018', '02-18-2018', '02-16-2018', '02-11-2018', '02-06-2018', '02-03-2018']
sorted_dates = sorted(datetime.strptime(d, '%m-%d-%Y') for d in dates)

time_difference = timedelta(0)
counter = 0
for i in range(1, len(sorted_dates), 1):
    counter += 1
    time_difference += sorted_dates[i] - sorted_dates[i-1]


frequency = time_difference / counter
print(frequency.days) # 7 days

Using statistics.mean with the datetime module:

from datetime import datetime
from statistics import mean

def to_dt(x):
    return datetime.strptime(x, '%m-%d-%Y')

res = mean((to_dt(x) - to_dt(y)).days for x, y in zip(L, L[1:]))  # 7.22

If you are looking for the average as a float value, then you could do something like the following (since your list is already sorted).

from datetime import datetime

dates = ['01-23-2019', '01-19-2019', '01-12-2019', '12-30-2018', '12-28-2018', '12-20-2018', '11-21-2018', '11-09-2018', '10-26-2018', '10-12-2018', '09-30-2018', '09-16-2018', '09-06-2018', '08-31-2018', '08-15-2018', '08-12-2018', '08-09-2018', '07-30-2018', '07-27-2018', '07-24-2018', '07-20-2018', '07-17-2018', '07-14-2018', '07-08-2018', '07-06-2018', '06-30-2018', '06-26-2018', '06-13-2018', '06-08-2018', '06-06-2018', '05-28-2018', '05-21-2018', '05-19-2018', '05-11-2018', '05-08-2018', '05-03-2018', '05-01-2018', '04-12-2018', '04-05-2018', '03-31-2018', '03-27-2018', '03-10-2018', '03-06-2018', '02-25-2018', '02-21-2018', '02-18-2018', '02-16-2018', '02-11-2018', '02-06-2018', '02-03-2018']

def daysdiff(a, b):
    return (datetime.strptime(a, '%m-%d-%Y') - datetime.strptime(b, '%m-%d-%Y')).days

average = sum(daysdiff(a, b) for a, b in zip(dates, dates[1:])) / (len(dates) - 1)
print(average)
# OUTPUT
# 7.224489795918367

Iterates over consecutive pairs in your list by zipping the list with a slice of itself, gets the date difference in days and divides by the number of consective pairs to produce the average.

Four things here : The use timedelta (datetime), zip , list comprehension , and mean (statistics)

from datetime import datetime
from statistics import mean

deltas = [(datetime.strptime(d1, '%m-%d-%Y') - datetime.strptime(d2, '%m-%d-%Y')).days 
          for d1, d2 in zip(my_list[:-1], my_list[1:])]
avg_days = mean(deltas)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM