Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas `value_counts` on a rolling time window

I have a pandas dataframe containing string values and a datetime index, like so:

from datetime import datetime as dt
import pandas as pd

df = pd.DataFrame(['a', 'b', 'b', 'c', 'b', 'b', 'b'], 
                  [dt(2019, 1, 1), dt(2019, 1, 2), 
                   dt(2019, 1, 3), dt(2019, 1, 4), 
                   dt(2019, 1, 5), dt(2019, 1, 6), 
                   dt(2019, 1, 7)])

If I wanted to compute the number of instances that each value occurs over all times, I can simply call:

>>> print(df[0].value_counts())
b    5
c    1
a    1
Name: 0, dtype: int64

I'd like to create a rolling window and measure the number of instances of each string on a moving window of say, 2 days. Is there a way to combine rolling with value_counts, or similar?

like image 755
Brett Morris Avatar asked Oct 28 '25 01:10

Brett Morris


1 Answers

I guess what you are looking for is:

pd.get_dummies(df[0]).rolling('2D').sum()

Output:

            a   b   c
2019-01-01  1.0 0.0 0.0
2019-01-02  1.0 1.0 0.0
2019-01-03  0.0 2.0 0.0
2019-01-04  0.0 1.0 1.0
2019-01-05  0.0 1.0 1.0
2019-01-06  0.0 2.0 0.0
2019-01-07  0.0 2.0 0.0
like image 131
Quang Hoang Avatar answered Oct 30 '25 17:10

Quang Hoang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!