I'm trying to return a count on a time window about a (moving) fixed point.
It's an attempt to understand the condition of an instrument at any time, as a function of usage prior to it.
So if the instrument is used at 12.05pm, 12.10, 12.15, 12.30, 12.40 and 1pm, the usage counts would be:
12.05 -> 1 (once in the last hour)
12.10 -> 2
12.15 -> 3
12.30 -> 4
12.40 -> 5
1.00 -> 6
... but then lets say usage resumes at 1.06: 1.06 -> 6 this doesn't increase the count, as the first run is over an hour ago.
How can I calculate this count and append it as a column?
It feels like this is an groupby/aggregate/count using possibly timedeltas in a lambda function, but I don't know where to start past that.
I'd like to be able to play with the time window too, so not just the past hour, but the hour surrounding an instance i.e. + and -30 minutes.
The following code gives a starting dataframe:
s = pd.Series(pd.date_range('2020-1-1', periods=8000, freq='250s'))
df = pd.DataFrame({'Run time': s})
df_sample = df.sample(6000)
df_sample = df_sample.sort_index()
The best help i found (and to be fair i can usually hack together from the logic) was this Distinct count on a rolling time window but i've not managed this time.
Thanks