我需要根据输入的时间找到master的时间范围。 cust_id 开始时间 0 1 2000-01-01 09:00:03 1 2 2000-01-01 18:01:03
我需要的输出是 cust_id 开始时间时间范围 0 1 2000-01-01 09:00:03 上午 1 2 2000-01-01 18:01:03 晚上
用于创建主时间范围详细信息的代码 mastdf={'时间范围':['早上','晚间','中午','晚上'],'start_time':['8:00:00','11:00:00','13:00 :00','17:00:00'],'end_time':['10:59:59','13:59:59','16:59:59','7:59:59'] }
用于创建输入数据框的代码 inputdf={'cust_id':[1,2],'starttime':['2000-01-01 09:00:03', '2000-01-01 18:01:03']}
cut
进行分箱,但首先按 to_timedelta
将值转换为时间增量,使用添加端点 24H
创建分箱,对于 00:00:00
到 8:00:00
之间的时间范围,使用 fillna
的最后一个值栏timeframe
:
mastdf={'timeframe':['morning','latemorning','midnoon','evening'],
'start_time':['8:00:00','11:00:00','13:00:00','17:00:00'],
'end_time':['10:59:59','13:59:59','16:59:59','7:59:59']}
mastdf = pd.DataFrame(mastdf)
print (mastdf)
timeframe start_time end_time
0 morning 8:00:00 10:59:59
1 latemorning 11:00:00 13:59:59
2 midnoon 13:00:00 16:59:59
3 evening 17:00:00 7:59:59
inputdf={'cust_id':[1,2],'starttime':['2000-01-01 09:00:03', '2000-01-01 18:01:03']}
inputdf = pd.DataFrame(inputdf)
inputdf['starttime'] = pd.to_datetime(inputdf['starttime'])
start = pd.to_timedelta(mastdf['start_time']).tolist() + [pd.Timedelta(24, unit='h')]
s = pd.to_timedelta(inputdf['starttime'].dt.strftime('%H:%M:%S'))
last = mastdf['timeframe'].iat[-1]
inputdf['timeframe'] = pd.cut(s,
bins=start,
labels=mastdf['timeframe'], right=False).fillna(last)
print (inputdf)
cust_id starttime timeframe
0 1 2000-01-01 09:00:03 morning
1 2 2000-01-01 18:01:03 evening