Skip to content
Advertisement

How many records include each time interval in Pandas?

I have two Pandas event dataframes. The first is for longer events, while the second is for shorter events. The start and end times of the events define them. Every long event is a “container” of at least one short event. Each short event is part of only one long event interval.

I try to figure out how many short events/intervals are in each long event.


Minimal example:

JavaScript

The desired output is:

JavaScript

I attempted to use “apply”, so I created a function that looked like this:

JavaScript

But I’m not sure how to proceed; should I use this function on the short events tables or the long events tables? How can I calculate the desired count per record in one table (the long events dataframe) based on the records in the other table (the short events dataframe)?

Advertisement

Answer

We can use numpy broadcasting here

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement