Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas rolling: aggregate boolean values

Is there any rolling "any" function in a pandas.DataFrame? Or is there any other way to aggregate boolean values in a rolling function?

Consider:

import pandas as pd
import numpy as np

s = pd.Series([True, True, False, True, False, False, False, True])

# this works but I don't think it is clear enough - I am not
# interested in the sum but a logical or!
s.rolling(2).sum() > 0  

# What I would like to have:
s.rolling(2).any()
# AttributeError: 'Rolling' object has no attribute 'any'
s.rolling(2).agg(np.any)
# Same error! AttributeError: 'Rolling' object has no attribute 'any'

So which functions can I use when aggregating booleans? (if numpy.any does not work) The rolling documentation at https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.rolling.html states that "a Window or Rolling sub-classed for the particular operation" is returned, which doesn't really help.

like image 486
Raubtier Avatar asked Sep 08 '26 22:09

Raubtier


1 Answers

You aggregate boolean values like this:

# logical or
s.rolling(2).max().astype(bool)

# logical and
s.rolling(2).min().astype(bool)

To deal with the NaN values from incomplete windows, you can use an appropriate fillna before the type conversion, or the min_periods argument of rolling. Depends on the logic you want to implement.

It is a pity this cannot be done in pandas without creating intermediate values as floats.

like image 151
adr Avatar answered Sep 10 '26 10:09

adr



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!