I am familiar with the Pandas Rolling window functions, but they always have a step size of 1. I want to do a moving aggregate function in Pandas, but where the entries don't overlap.
In this Dataframe:
df.rolling(2).min()
will yield:
N/A 519 566 727 1099 12385
But I want a fixed window with a step size of 2, so it yields:
519 727 12385
Because with a fixed window, it should step over by the size of that window instead.
There's no such built in argument in the rolling
function, but you can compute the usual rolling function and then skip every n
th row (where n=2
in your case).
df.rolling(n).min()[n-1::n]
As you mentioned in your comment, this might result in many redudant computations which will be ignored (especially if n
is large).
Instead, you could use the following code which partitions (groups) the data into bins of size n
:
df.groupby(df.index // n).min()
I did not check if it's indeed more efficient, but I believe it should be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With