[英]Time period over days
I have the following DataFrame:我有以下 DataFrame:
import pandas as pd
data = {'ID': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
'Time_order': ['2019-01-01 07:00:00', '2019-01-01 07:25:00', '2019-01-02 07:02:00', '2019-01-02 07:27:00', '2019-01-02 06:58:00', '2019-01-03 07:24:00', '2019-01-04 07:03:00', '2019-01-04 07:24:00', '2019-01-05 07:05:00', '2019-01-05 07:30:00', '2019-01-06 07:00:00', '2019-01-06 07:25:00', '2019-01-07 07:02:00', '2019-01-07 07:27:00', '2019-01-08 06:58:00', '2019-01-08 07:24:00', '2019-01-09 07:03:00', '2019-01-09 07:24:00', '2019-01-10 07:05:00', '2019-01-10 07:30:00',
'2019-01-11 017:00:00', '2019-01-11 17:25:00', '2019-01-12 07:02:00', '2019-01-12 07:27:00', '2019-01-13 06:58:00', '2019-01-13 07:24:00', '2019-01-14 07:03:00', '2019-01-14 07:24:00', '2019-01-15 07:05:00', '2019-01-15 07:30:00']}
df = pd.DataFrame(data)
df['Time_order'] = pd.to_datetime(df['Time_order'])
df['hour'] = df['Time_order'].dt.strftime('%H:%M:%S)
I wanted to make a time_period = 25 minutes
of length 25 minutes, so that I can check wether there are orders in that time_period.我想制作一个长度为 25 分钟的
time_period = 25 minutes
分钟,以便我可以检查该 time_period 内是否有订单。 For example: I will start checking everyday starting from midnight, eg from 00:00:00
to 00:25:00
and claculate how many orders are in that order and then move on by a 5 minutes, eg from 00:05:00
to 00:30:00
and so on scanning the whole day until 23:59:00
.例如:我将从午夜开始每天检查,例如从
00:00:00
到00:25:00
并计算该订单中有多少订单,然后继续前进 5 分钟,例如从00:05:00
到00:30:00
等全天扫描,直到23:59:00
。 What I am expecting is how many orders where made and pick the maximum ones, so that it returns at which time there is a peak of orders during that time period.我期望的是制作了多少订单并选择最多的订单,以便在该时间段内出现订单高峰时返回。
I tired the following:我厌倦了以下内容:
x = 12 * 24 # each five minutes per hour (12) times 24 hours (a day)
for i in range(x):
df[f'each{i}_minutes_start'] = pd.to_datetime(df['Time_order']).dt.floor(f'{i}_min')
df[f'each{i}_minutes_end'] = df[f'each{i}_minutes_start'] + pd.Timedelta(minutes = 5)
df['time_period'] = df[f'each{i}_minutes_start'].dt.strftime('%H:%M:S%') + '-' + pd.to_datetime(df[f'each{i}_minutes_end']).dt.strtime('%H:%M:S%')
at this point I stucked and could not come forward.在这一点上,我卡住了,无法挺身而出。 Thank you in advance
先感谢您
I think this works:我认为这有效:
df.set_index('Time_order').resample("5min").count().rolling(6)['ID'].sum()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.