【发布时间】:2017-10-06 13:10:42
【问题描述】:
我正在尝试使用来自this 线程的代码从列表中计算平均时间。所有其他代码建议都不适合我,因为它们考虑的是持续时间而不是时间。
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)
我收到以下错误:
TypeError: integer argument expected, got float
有人可以帮忙吗?
【问题讨论】:
-
运行代码并获得结果
datetime.time(0, 15)和datetime.time(0, 0)没有问题。你可以重启你的机器再试一次。 -
还是一样。我得到了 Python 3.6.2 (Anaconda)。有什么想法吗?
-
在下面查看我的回复。
标签: python