#Pandas, Numpy, time buckets, binning, VBA equivalent
import numpy as np
import pandas as pd
rng = pd.date_range('2000-01-01', periods=12, freq='T')
ts = pd.Series(np.arange(12), index=rng)
ts
ts.resample('5min', closed='right', label='right', loffset='-1s').sum()
start_times = ['2000-01-01 09:00', '2000-01-01 10:00']
end_times = ['2000-01-01 17:00', '2000-01-01 18:00']
index = ['Timeframe ' + str(i) for i in range(len(start_times))]
df = pd.DataFrame({'Start Time': pd.to_datetime(start_times),
'End Time' : pd.to_datetime(end_times)}, index=index)
df
df['Start Time']
df.unstack()
rng = pd.date_range('2000-01-01 09:00', periods=9, freq='H')
rng
ts = pd.DataFrame(0, index=rng, columns=['minutes'], dtype='float')
ts
from pandas.tseries.offsets import Hour, Minute
from IPython.core.debugger import set_trace
for index, row in ts.iterrows():
#set_trace()
start_boundary = index
end_boundary = index + Hour()
time_count = pd.Timedelta('0 m')
for _, raw_data in df.iterrows():
#set_trace()
start_time = raw_data['Start Time']
end_time = raw_data['End Time']
if end_time > start_boundary:
if start_time < end_boundary:
if start_time <= start_boundary:
if end_time >= end_boundary:
time_count = time_count + (end_boundary - start_boundary)
else:
time_count = time + (end_time - start_boundary)
else:
if end_time >= end_boundary:
time_count = time_count + (end_boundary - start_time)
else:
time_count = time_count + (end_time - start_time)
#set_trace()
ts.at[index, 'minutes'] = time_count.seconds / 60
ts