Before everyone down votes, this is a tricky question to phrase in a single title. For a given timestamp, I want to round it to the previous 15 min when it's more than 10 mins away (i.e 11-15 mins). If it's less than 10 mins away I want to round that to the previous, previous 15 min.
This may be easier to display:
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
I can round values greater than 10 mins easily enough. The ones less than 10 mins is where I'm struggling. I attempted to convert the timestamps to total seconds to determine if it's less than 600 seconds (10 mins). If less than 600 seconds I would take another 15 mins off. If more than 600 seconds I would leave as is. Below is my attempt.
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