I have a pandas series of a bunch of 32 bit integers, and I'd like to roll through with series.rolling and get the bitwise-and of all the values in the window, basically trying to do this except that obviously bitwise_and is not a real pandas function one can call:
df = pd.Series(\[0xFFFF, 0xFEAB, 0x0000, 0x1111, 0x5555, 0xAAAA\], dtype='int32')
df.rolling(2).bitwise_and()
So far the best method I can find to do this is defining a custom function and using .apply(), as below:
def accumulate_bitwise_and(values):
result = np.uint32(0xFFFFFFFF)
for v in values:
# Upstream Pandas rolling window functionality converts
# inputs to floating point types, hence the conversion
result &= np.uint32(v)
return result
accum = df.rolling(2).apply(accumulate_bitwise_and)
accum.drop(accum.index[0], inplace=True) # Drop expected NaN
accum = accum.astype("uint32")
But it seems like there's likely to be a better way?