- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想在图形中可视化特定时间作业车间中所需机器的数量,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 分钟后的结果:
最大的:
骑士:
编辑 2 2015 年 4 月 4 日
最佳答案
这并不容易。我真的想不出一种完全矢量化的方法,但这里有 2 种可行的方法。
1- 重新组织您的数据,使您只有一个日期时间列。目标是对于每个 shift_ID,每个最小间隔 一行。然后你就可以到一个timegrouper groupby。
工作示例:
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
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
# 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
创建日期列移动一个的副本)。
** 编辑
关于python - 计算时间跨度类次所需的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337924/
我有一个依赖于包 B 的包 A。当包 A 中的代码运行并访问包 B 中的类时,包 B 的状态将被解析 (4),而不是 Activity (32) 和包 B 的激活器也没跑好。我认为 bundle B
这个问题在这里已经有了答案: How to remove the space between inline/inline-block elements? (41 个回答) 关闭 7 年前。
我正在尝试使用 Java OpenAL 库。我在导入名为 libsoft_oal.so 的 native 库时遇到问题。 Java OpenAL 依赖于 OpenAL 软实现。我尝试根据他们在 git
我正在尝试启动我的应用程序。是一个 unicorn +工头+sinatra的应用。 这是我的 config.ru 文件: require "rubygems" require "sinatra" Bu
我有一个下拉列表,其中包含一些从数据库表中检索的值,我想要的是当单击按钮时它应该只获得选项标签的中间值,但只有那些类名为“get_this”的选项标签并离开那些选项,如果他们没有这个类 预期输出:值
我有一个index.php文件,需要一个通用的head.php文件,head.php文件中有几个Javascript文件,当这样尝试时,代码在源代码中看起来很好,但文件却不是实际上对文档做任何事情。
有人能帮帮我吗? 我已经像这样运行了 imsmod: $ insmod /data/mm/mmdev.ko epoll_rate=100 但是我得到一个错误: insmod: init_module
是否有键盘快捷键或插件可以在 Notepad++ 中打开 PHP 所需或包含的文件?我知道,在 Dreamweaver 中,执行此操作的命令是 Ctrl+D,但我似乎无法在 Notepad++ 中找到
我已经用 js 设置了一个显示/隐藏 div,但我很难弄清楚如何一次显示一个 div。目前发生的情况是,除非我再次单击原始链接来关闭该 div,否则每个 div 都会显示。 http://www.li
当我尝试将未分配的辅助分片分配给节点时出现错误。 { "error": { "root_cause": [ { "type": "remote_transpor
我正在构建一个 C++ 应用程序,使用 Netbeans 6.9 作为我的 IDE。我有一个 C++ 库,它是一个纯 C 库的包装器。 我已将文件正确添加到项目中(使用添加库文件选项)。这是 g++
我是一名优秀的程序员,十分优秀!