首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算列表产量误差的平均时间

问计算列表产量误差的平均时间
EN

Stack Overflow用户
提问于 2017-10-06 13:10:42
回答 3查看 94关注 0票数 0

我试图使用this线程的代码从列表中计算出平均时间。所有其他代码建议都不适用于我,因为它们考虑的是持续时间而不是时间。

代码语言:javascript
复制
import datetime
import math
import numpy

def datetime_to_radians(x):
    # radians are calculated using a 24-hour circle, not 12-hour, starting at north and moving clockwise
    time_of_day = x.time()
    seconds_from_midnight = 3600 * time_of_day.hour + 60 * time_of_day.minute + time_of_day.second
    radians = float(seconds_from_midnight) / float(12 * 60 * 60) * 2.0 * math.pi
    return radians

def average_angle(angles):
    # angles measured in radians
    x_sum = numpy.sum([math.sin(x) for x in angles])
    y_sum = numpy.sum([math.cos(x) for x in angles])
    x_mean = x_sum / float(len(angles))
    y_mean = y_sum / float(len(angles))
    return numpy.arctan2(x_mean, y_mean)

def radians_to_time_of_day(x):
    # radians are measured clockwise from north and represent time in a 24-hour circle
    seconds_from_midnight = int(float(x) / (2.0 * math.pi) * 12.0 * 60.0 * 60.0)
    hour = seconds_from_midnight / 3600
    minute = (seconds_from_midnight % 3600) / 60
    second = seconds_from_midnight % 60
    return datetime.time(hour, minute, second)

def average_times_of_day(x):
    # input datetime.datetime array and output datetime.time value
    angles = [datetime_to_radians(y) for y in x]
    avg_angle = average_angle(angles)
    return radians_to_time_of_day(avg_angle)

average_times_of_day([datetime.datetime(2017, 6, 9, 0, 10), datetime.datetime(2017, 6, 9, 0, 20)])
# datetime.time(0, 15)

average_times_of_day([datetime.datetime(2017, 6, 9, 23, 50), datetime.datetime(2017, 6, 9, 0, 10)])
# datetime.time(0, 0)

我得到了以下错误:

代码语言:javascript
复制
TypeError: integer argument expected, got float

有人能帮忙吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-06 15:02:32

下面是另一种可以处理日期时间列表的解决方案--仅考虑小时和分钟的对象:

代码语言:javascript
复制
from cmath import phase
from cmath import rect
import datetime
from math import degrees
from math import radians

dfList = ([datetime.datetime(2017, 9, 15, 8, 8),
           datetime.datetime(2017, 9, 14, 8, 5),
           datetime.datetime(2017, 9, 13, 6, 56),
           datetime.datetime(2017, 12, 9, 6, 14),
           datetime.datetime(2017, 11, 9, 6, 42)])

dfList = [item.strftime("%H:%M") for item in dfList]


def mean_angle(deg):
    return degrees(phase(sum(rect(1, radians(d)) for d in deg) / len(deg)))


def mean_time(times):
    t = (time.split(':') for time in times)
    seconds = ((int(m) * 60 + int(h) * 3600)
               for h, m in t)
    day = 24 * 60 * 60
    to_angles = [s * 360. / day for s in seconds]
    mean_as_angle = mean_angle(to_angles)
    mean_seconds = mean_as_angle * day / 360.
    if mean_seconds < 0:
        mean_seconds += day
    h, m = divmod(mean_seconds, 3600)
    m, s = divmod(m, 60)
    return '%02i:%02i' % (h, m)


print(mean_time(dfList))
票数 1
EN

Stack Overflow用户

发布于 2017-10-06 14:12:39

您的代码在Python2.7中运行得很好,但Python3.6却不行。

错误抛到第26行,return datetime.time(hour, minute, second)。

将hour、minute和second的值转换为int()后,工作正常。

代码语言:javascript
复制
def radians_to_time_of_day(x):
    # radians are measured clockwise from north and represent time in a 24-hour circle
    seconds_from_midnight = int(float(x) / (2.0 * math.pi) * 12.0 * 60.0 * 60.0)
    hour = int(seconds_from_midnight / 3600)
    minute = int((seconds_from_midnight % 3600) / 60)
    second = int(seconds_from_midnight % 60)
    return datetime.time(hour, minute, second)
票数 0
EN

Stack Overflow用户

发布于 2017-10-06 14:13:37

在Python3中,即使两个操作数都是整数,/也执行真(浮动)除法。

使用//代替

代码语言:javascript
复制
def radians_to_time_of_day(x):
    # radians are measured clockwise from north and represent time in a 24-hour circle
    seconds_from_midnight = int(float(x) / (2.0 * math.pi) * 12.0 * 60.0 * 60.0)
    hour = seconds_from_midnight // 3600
    minute = (seconds_from_midnight % 3600) // 60
    second = seconds_from_midnight % 60
    return datetime.time(hour, minute, second)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46606532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档