4

I have two columns; the time an event started and the duration of that event. Like so:

time, duration
1:22:51,41
1:56:29,36
2:02:06,12
2:32:37,38
2:34:51,24
3:24:07,31
3:28:47,59
3:31:19,32
3:42:52,37
3:57:04,58
4:21:55,23
4:40:28,17
4:52:39,51
4:54:48,26
5:17:06,46
6:08:12,1
6:21:34,12
6:22:48,24
7:04:22,1
7:06:28,46
7:19:12,51
7:19:19,4
7:22:27,27
7:32:25,53

I want to create a line chart that shows the number of concurrent events happening throughout the day. Renaming time to start_time and adding a new column that computes the end_time is easy enough (assuming that's the next step) -- what I'm not quite sure I understand is how, afterwards, I can resample this data so I can chart concurrents.

I imagine I want to wind up with something like (but bucketed by the minute):

time, events
1:30:00,1
2:00:00,2
2:30:00,1
3:00:00,1
3:30:00,2
keshlam
  • 7,931
  • 2
  • 19
  • 33
profesor_tortuga
  • 1,826
  • 2
  • 17
  • 27

2 Answers2

6

First make it an actual time stamp:

df['time'] = pd.to_datetime('2014-03-14 ' + df['time'])

Now you can get the end times:

df['end_time'] = df['time'] + df['duration'] * pd.offsets.Minute(1)

A way to get the open events is to combine the start and end times, resample and cumsum:

In [11]: open = pd.concat([pd.Series(1, df.time),  # created add 1
                           pd.Series(-1, df.end_time)  # closed substract 1
                           ]).resample('30Min', how='sum').cumsum()

In [12]: open
Out[12]:
2014-03-14 01:00:00    1
2014-03-14 01:30:00    2
2014-03-14 02:00:00    1
2014-03-14 02:30:00    1
2014-03-14 03:00:00    2
2014-03-14 03:30:00    4
2014-03-14 04:00:00    2
2014-03-14 04:30:00    2
2014-03-14 05:00:00    2
2014-03-14 05:30:00    1
2014-03-14 06:00:00    2
2014-03-14 06:30:00    0
2014-03-14 07:00:00    3
2014-03-14 07:30:00    2
2014-03-14 08:00:00    0
Freq: 30T, dtype: int64
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
2

You could create a list containing dictionary items with values "time", "events"

obviously you need to handle the evaluating and manipulating of time data types differently, but you could do something like this:

 event_bucket = []
 time_interval = (end_time - start_time) / num_of_buckets
 for ii in range(num_of_buckets):
      event_bucket.append({"time":start_time + ii*time_interval,"events":0})

 for entry in time_entry:
      for bucket in event_bucket:
            if  bucket["time"] >= entry["start_time"] and bucket["time"] <= entry["end_time"]:
                  bucket["events"] += 1

If you make num_of_buckets larger you make the graph more precise.

flakes
  • 21,558
  • 8
  • 41
  • 88