gpt4 book ai didi

python - 计算时间跨度类次所需的设备

转载 作者:太空狗 更新时间:2023-10-30 01:27:52 24 4
gpt4 key购买 nike

我想在图形中可视化特定时间作业车间中所需机器的数量,x 轴是连续时间轴,y 轴是类次数。

在下面的数据框中,您可以找到我的数据示例。在这里,您会看到 Shift_ID(它们是唯一的)以及该类次的开始和结束时间。在一天的一段时间内,我想看看在某个时间间隔内需要多少台机器。这可以是 5 分钟、一刻钟、半小时和小时。

df:
Shift_ID Shift_Time_Start Shift_Time_End
0 1 2016-03-22 9:00:00 2016-03-22 9:35:00
1 2 2016-03-22 9:20:00 2016-03-22 10:20:00
2 3 2016-03-22 9:40:00 2016-03-22 10:14:00
3 4 2016-03-22 10:00:00 2016-03-22 10:31:00

在这个季度 9:30-9:45 的示例中,我需要 3 台机器才能在那个特定时间完成每个类次。所需的输出看起来像这样:

df2:

Interval Count
0 2016-03-22 9:00:00 - 2016-03-22 9:15:00 1
1 2016-03-22 9:15:00 - 2016-03-22 9:30:00 2
2 2016-03-22 9:30:00 - 2016-03-22 9:45:00 3
3 2016-03-22 9:45:00 - 2016-03-22 10:00:00 2
4 2016-03-22 10:00:00 - 2016-03-22 10:15:00 2
5 2016-03-22 10:15:00 - 2016-03-22 10:30:00 2
6 2016-03-22 10:30:00 - 2016-03-22 10:45:00 1

有了这个数据框,我可以将它四舍五入到区间的最低边界,然后将它绘制成图表。

我卡在了如何“查看”一个类次是否位于多个区间内。您有解决此问题的想法吗?

注意:所有日期时间值当然都是日期时间类型

在 MaxU 和 knightofni 的解决方案之后编辑

我使用 MaxU 的代码绘制了您的代码。他们似乎都在 15 分钟内做得很好,但请看看你在 5 分钟后的结果:

最大的:

enter image description here

骑士:

enter image description here

编辑 2 2015 年 4 月 4 日

最佳答案

这并不容易。我真的想不出一种完全矢量化的方法,但这里有 2 种可行的方法。

1- 重新组织您的数据,使您只有一个日期时间列。目标是对于每个 shift_ID,每个最小间隔 一行。然后你就可以到一个timegrouper groupby。

工作示例:

重新创建您的 DataFrame

import pandas as pd
import arrow

data = {
'Shift_ID' : [1,2,3,4],
'Shift_Time_Start' : [arrow.get('2016-03-22 09:00:00').datetime,
arrow.get('2016-03-22 09:20:00').datetime,
arrow.get('2016-03-22 09:40:00').datetime,
arrow.get('2016-03-22 10:00:00').datetime
],

'Shift_Time_End' : [arrow.get('2016-03-22 09:35:00').datetime,
arrow.get('2016-03-22 10:20:00').datetime,
arrow.get('2016-03-22 10:14:00').datetime,
arrow.get('2016-03-22 10:31:00').datetime
],
}


df = pd.DataFrame(data)
min_int = '5T'
df

Shift_ID Shift_Time_End Shift_Time_Start
0 1 2016-03-22 09:35:00+00:00 2016-03-22 09:00:00+00:00
1 2 2016-03-22 10:20:00+00:00 2016-03-22 09:20:00+00:00
2 3 2016-03-22 10:14:00+00:00 2016-03-22 09:40:00+00:00
3 4 2016-03-22 10:31:00+00:00 2016-03-22 10:00:00+00:00

创建新的 Df

new_data = {'time' : [], 'Shift_ID': []} # dict to hold the data

for row in df.iterrows():
# creates a list of all dates of this shift, from start to end
dates = pd.date_range(row[1].Shift_Time_Start, row[1].Shift_Time_End, freq=min_int)
for date in dates:
new_data['time'].append(date)
new_data['Shift_ID'].append(row[1].Shift_ID)

# creating the new df
newdf = pd.DataFrame(new_data).set_index('time')
newdf.head()


Shift_ID
time
2016-03-22 09:00:00+00:00 1
2016-03-22 09:05:00+00:00 1
2016-03-22 09:10:00+00:00 1
2016-03-22 09:15:00+00:00 1
2016-03-22 09:20:00+00:00 1

Groupby 时间分组器

# We groupby the time column, resampling every min_int 
# (in our case 5 minutes, represented by '5T'),
# then we check how many uniquer shift_id.
newdf.groupby(pd.TimeGrouper(freq=min_int)).agg({'Shift_ID': lambda x : len(set(x))})

Shift_ID
time
2016-03-22 09:00:00+00:00 1
2016-03-22 09:05:00+00:00 1
2016-03-22 09:10:00+00:00 1
2016-03-22 09:15:00+00:00 1
2016-03-22 09:20:00+00:00 2
2016-03-22 09:25:00+00:00 2
2016-03-22 09:30:00+00:00 2
2016-03-22 09:35:00+00:00 2
2016-03-22 09:40:00+00:00 2

这表示 在 9:15 有一个轮类,而在 9:20 有 2 个

这不完全是您想要的输出,但我认为这更容易绘制。如果你想匹配你想要的输出,这应该很容易(只需使用 .shift 创建日期列移动一个的副本)。

** 编辑

Link to notebook with code

关于python - 计算时间跨度类次所需的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com