计算日期列表之间的平均天数

时间:2019-01-25 14:41:13

标签: python python-3.x datetime average

我正在尝试计算用户在Instagram上的发布频率。因此,我建立了他们最近发布日期的列表。像这样:

['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']

理想情况下,我想获取发布日期之间的平均天数。所以我最终得到一个频率数字:例如“用户每 n 天发布一次”。

我正在从JSON代码获取时间戳并将其转换为类似这样的可读性:

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)

如何最好地获得十进制结果?

4 个答案:

答案 0 :(得分:2)

只需将日期之间的差异求和,然后除以总数即可。为简单起见

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

答案 1 :(得分:2)

statistics.meandatetime模块一起使用:

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

答案 2 :(得分:1)

如果您希望将平均值作为浮点值,则可以执行以下操作(因为列表已经排序)。

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

在列表中的连续对上进行迭代,方法是将列表的一部分切成薄片,获得以天为单位的日期差,然后除以连续对的数量以得出平均值。

答案 3 :(得分:1)

这里有四件事:使用timedelta(日期时间),ziplist comprehension和均值(统计信息)

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)