gpt4 book ai didi

python - python 中的日期时间差异针对夜间进行了调整

转载 作者:太空狗 更新时间:2023-10-29 20:53:19 25 4
gpt4 key购买 nike

我在 python d1 和 d2 中有两个日期时间对象。我想考虑他们之间的时差。我想要比 (d1 - d2) 稍微复杂一点的东西:我希望夜间的时间比白天的时间少一个常数分数 c,例如晚上一小时只算白天半小时。

在 python(pandas 和/或 numpy)中有一个简单的方法吗?

谢谢!

编辑:晚上 9 点到早上 7 点。但理想情况下,我正在寻找一个解决方案,您可以在白天的任意时间选择任意权重

最佳答案

此解决方案计算完整日期的加权数,然后从第一个日期和最后一个日期中减去或添加任何残差。这不考虑任何夏令时效应。

import pandas as pd


def timediff(t1, t2):

DAY_SECS = 24 * 60 * 60
DUSK = pd.Timedelta("21h")
# Dawn is chosen as 7 a.m.
FRAC_NIGHT = 10 / 24
FRAC_DAY = 14 / 24
DAY_WEIGHT = 1
NIGHT_WEIGHT = 0.5

full_days = ((t2.date() - t1.date()).days * DAY_SECS *
(FRAC_NIGHT * NIGHT_WEIGHT + FRAC_DAY * DAY_WEIGHT))

def time2dusk(t):
time = (pd.Timestamp(t.date()) + DUSK) - t
time = time.total_seconds()
wtime = (min(time * NIGHT_WEIGHT, 0) +
min(max(time, 0), FRAC_DAY * DAY_SECS) * DAY_WEIGHT +
max(time - DAY_SECS * FRAC_DAY, 0) * NIGHT_WEIGHT)
return wtime

t1time2dusk = time2dusk(t1)
t2time2dusk = time2dusk(t2)
return full_days + t1time2dusk - t2time2dusk

这提供了加权秒数的解决方案,但您可以在之后转换为任何方便的方法

times = [(pd.Timestamp("20170101T12:00:00"), pd.Timestamp("20170101T15:00:00")),
(pd.Timestamp("20170101T12:00:00"), pd.Timestamp("20170101T23:00:00")),
(pd.Timestamp("20170101T12:00:00"), pd.Timestamp("20170102T12:00:00")),
(pd.Timestamp("20170101T22:00:00"), pd.Timestamp("20170101T23:00:00")),
(pd.Timestamp("20170101T22:00:00"), pd.Timestamp("20170102T05:00:00")),
(pd.Timestamp("20170101T06:00:00"), pd.Timestamp("20170101T08:00:00"))]

exp_diff_hours = [3, 9 + 2*0.5, 9 + 10*0.5 + 5, 1*0.5, 7*0.5, 1 + 1*0.5]

for i, ts in enumerate(times):
t1, t2 = ts
print("\n")
print("Time1: %s" % t1)
print("Time2: %s" % t2)
print("Weighted Time2 - Time1: %s" % (timediff(t1, t2) / 3600))
print("Weighted Time2 - Time1 Expected: %s" % exp_diff_hours[i])

for i, ts in enumerate(times):
t2, t1 = ts
print("\n")
print("Time1: %s" % t1)
print("Time2: %s" % t2)
print("Weighted Time2 - Time1: %s" % (timediff(t1, t2) / 3600))
print("Weighted Time2 - Time1 Expected: %s" % -exp_diff_hours[i])

Time1: 2017-01-01 12:00:00
Time2: 2017-01-01 15:00:00
Weighted Time2 - Time1: 3.000000000000001
Weighted Time2 - Time1 Expected: 3


Time1: 2017-01-01 12:00:00
Time2: 2017-01-01 23:00:00
Weighted Time2 - Time1: 10.0
Weighted Time2 - Time1 Expected: 10.0


Time1: 2017-01-01 12:00:00
Time2: 2017-01-02 12:00:00
Weighted Time2 - Time1: 19.0
Weighted Time2 - Time1 Expected: 19.0


Time1: 2017-01-01 22:00:00
Time2: 2017-01-01 23:00:00
Weighted Time2 - Time1: 0.5
Weighted Time2 - Time1 Expected: 0.5


Time1: 2017-01-01 22:00:00
Time2: 2017-01-02 05:00:00
Weighted Time2 - Time1: 3.5
Weighted Time2 - Time1 Expected: 3.5


Time1: 2017-01-01 06:00:00
Time2: 2017-01-01 08:00:00
Weighted Time2 - Time1: 1.5
Weighted Time2 - Time1 Expected: 1.5


Time1: 2017-01-01 15:00:00
Time2: 2017-01-01 12:00:00
Weighted Time2 - Time1: -3.000000000000001
Weighted Time2 - Time1 Expected: -3


Time1: 2017-01-01 23:00:00
Time2: 2017-01-01 12:00:00
Weighted Time2 - Time1: -10.0
Weighted Time2 - Time1 Expected: -10.0


Time1: 2017-01-02 12:00:00
Time2: 2017-01-01 12:00:00
Weighted Time2 - Time1: -19.0
Weighted Time2 - Time1 Expected: -19.0


Time1: 2017-01-01 23:00:00
Time2: 2017-01-01 22:00:00
Weighted Time2 - Time1: -0.5
Weighted Time2 - Time1 Expected: -0.5


Time1: 2017-01-02 05:00:00
Time2: 2017-01-01 22:00:00
Weighted Time2 - Time1: -3.5
Weighted Time2 - Time1 Expected: -3.5


Time1: 2017-01-01 08:00:00
Time2: 2017-01-01 06:00:00
Weighted Time2 - Time1: -1.5
Weighted Time2 - Time1 Expected: -1.5

关于python - python 中的日期时间差异针对夜间进行了调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43337208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com