四舍五入到前15分钟,除非10分钟内

时间:2019-03-06 06:45:06

标签: python pandas time rounding

在每个人都投反对票之前,用单个标题说这是一个棘手的问题。对于给定的时间戳,我想将其四舍五入到前15分钟(如果距离超过10分钟(即11-15分钟))。如果距离不到10分钟,我想将其四舍五入到上一个15分钟。

这可能更容易显示:

1st timestamp = 08:12:00. More than 10 mins so round to nearest 15 min = 08:00:00
2nd timestamp = 08:07:00. Less than 10 mins so round to the previous, previous 15 min = 7:45:00

我可以很容易地舍入大于10分钟的值。不到10分钟的时间就是我要努力的地方。我试图将时间戳转换为总秒数,以确定是否少于600秒(10分钟)。如果少于600秒,我还要再休息15分钟。如果超过600秒,我将原样离开。以下是我的尝试。

import pandas as pd
from datetime import datetime, timedelta

d = ({
    'Time' : ['8:10:00'],                                                                                          
     })

df = pd.DataFrame(data=d)

df['Time'] = pd.to_datetime(df['Time'])

def hour_rounder(t):
    return t.replace(second=0, microsecond=0, minute=(t.minute // 15 * 15), hour=t.hour)

FirstTime = df['Time'].iloc[0]
StartTime = hour_rounder(FirstTime)

#Strip date
FirstTime = datetime.time(FirstTime)
StartTime = datetime.time(StartTime)

#Convert timestamps to total seconds
def get_sec(time_str):
    h, m, s = time_str.split(':')
    return int(h) * 3600 + int(m) * 60 + int(s)

FirstTime = str(FirstTime)
FirstTime_secs = get_sec(FirstTime)

StartTime = str(StartTime)
StartTime_secs = get_sec(StartTime)

#Determine difference
diff = FirstTime_secs - StartTime_secs 

1 个答案:

答案 0 :(得分:2)

如果可能使用时间增量,请先使用to_timedelta,然后使用Series.dt.floor,如果模15小于或等于10,则删除15分钟:

d = {'Time': ['08:00:00', '08:01:00', '08:02:00', '08:03:00', '08:04:00', 
              '08:05:00', '08:06:00', '08:07:00', '08:08:00', '08:09:00', 
              '08:10:00', '08:11:00', '08:12:00', '08:13:00', '08:14:00', 
              '08:15:00', '08:16:00', '08:17:00', '08:18:00', '08:19:00',
              '08:20:00', '08:21:00', '08:22:00', '08:23:00', '08:24:00', 
              '08:25:00', '08:26:00', '08:27:00', '08:28:00', '08:29:00',
              '08:30:00', '08:31:00', '08:32:00', '08:33:00', '08:34:00', 
              '08:35:00', '08:36:00', '08:37:00', '08:38:00', '08:39:00']}

df = pd.DataFrame(d)

df['Time'] = pd.to_timedelta(df['Time'])

s = df['Time'].dt.floor(freq='15T')
#https://stackoverflow.com/a/14190143 for convert timedeltas to minutes
df['new'] = np.where(((df['Time'].dt.total_seconds() % 3600) // 60) % 15 <= 10, 
                      s - pd.Timedelta(15 * 60, 's'), s)
print (df)

       Time      new
0  08:00:00 07:45:00
1  08:01:00 07:45:00
...
9  08:09:00 07:45:00
10 08:10:00 07:45:00
11 08:11:00 08:00:00
12 08:12:00 08:00:00
...
24 08:24:00 08:00:00
25 08:25:00 08:00:00
26 08:26:00 08:15:00
27 08:27:00 08:15:00
...
38 08:38:00 08:15:00
39 08:39:00 08:15:00

如果需要使用日期时间解决方案,则与Series.dt.minute类似:

df = pd.DataFrame({'Time':pd.date_range('2015-01-01 08:00:00', freq='T', periods=40)})

s = df['Time'].dt.floor(freq='15T')
df['new'] = np.where(df['Time'].dt.minute % 15 <= 10, s - pd.Timedelta(15*60, 's'), s)

print (df)
                  Time                 new
0  2015-01-01 08:00:00 2015-01-01 07:45:00
1  2015-01-01 08:01:00 2015-01-01 07:45:00
...
9  2015-01-01 08:09:00 2015-01-01 07:45:00
10 2015-01-01 08:10:00 2015-01-01 07:45:00
11 2015-01-01 08:11:00 2015-01-01 08:00:00
12 2015-01-01 08:12:00 2015-01-01 08:00:00
13 2015-01-01 08:13:00 2015-01-01 08:00:00
...
24 2015-01-01 08:24:00 2015-01-01 08:00:00
25 2015-01-01 08:25:00 2015-01-01 08:00:00
26 2015-01-01 08:26:00 2015-01-01 08:15:00
27 2015-01-01 08:27:00 2015-01-01 08:15:00
...
38 2015-01-01 08:38:00 2015-01-01 08:15:00
39 2015-01-01 08:39:00 2015-01-01 08:15:00

评论的替代解决方案:

df['new1'] = df['Time'].sub(pd.Timedelta(11*60, 's')).dt.floor(freq='15T')