Re-sampling event time series + duration into parallel events

I have two columns; start time of the event and the duration of this event. For instance:

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 showing the number of simultaneous events that occur during the day. The time of renaming to start_timeand adding a new column that computes end_timeis simple enough (assuming the next step) - that I'm not quite sure, I understand how after that I can redo this data so that I can make up the reconciliation.

I guess I want to end up with something like (but soon):

time, events
1:30:00,1
2:00:00,2
2:30:00,1
3:00:00,1
3:30:00,2
+4
source share
2 answers

:

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

:

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

, :

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
+5

, "", ""

, -, - :

 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

num_of_buckets , .

+2

Source: https://habr.com/ru/post/1531781/


All Articles