pandas :重叠时间计数 [英] Pandas: Overlapping time count

查看:83
本文介绍了 pandas :重叠时间计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组具有多个开始和结束时间的数据,我正在使用熊猫寻找获取重叠的开始和结束时间(即发生的最大开始和结束时间)数量的最佳方法.

I have a set of data that has a bunch of start and end times, Using pandas i'm looking for the best way to get the number of overlapping start and end times, meaning the maximum start and end times that happen.

该表包含具有开始时间和结束时间的会话,其目的是创建一个折线图,以显示一段时间内会话的最大并发性.

The table is of sessions with start and end time, the idea is to create a line graph showing the maximum concurrency of sessions over time.

推荐答案

import numpy as np
import pandas as pd
ranges = np.asarray([(np.datetime64('2013-12-24T00:00:00Z'), np.datetime64('2013-12-24T00:02:00Z')),
                     (np.datetime64('2013-12-24T00:02:00Z'), np.datetime64('2013-12-24T00:04:00Z')),
                     (np.datetime64('2013-12-24T00:03:00Z'), np.datetime64('2013-12-24T00:05:00Z'))])
us = pd.Series(index=np.unique(ranges), data=0)
for start, end in ranges:
    us[start:end] += 1
print us

输出:

2013-12-24 00:00:00    1
2013-12-24 00:02:00    2
2013-12-24 00:03:00    2
2013-12-24 00:04:00    2
2013-12-24 00:05:00    1

注意:您可能希望以不同的方式处理边缘(例如,2013-12-24 00:02:00 2)

Note: you may want to handle the edges differently (e.g., 2013-12-24 00:02:00 2)

这篇关于 pandas :重叠时间计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆