I have a data frame with a variable "A" and I would like to create a rolling Nan checker, such that the new variable "rolling_nan" = 1 if ALL 3 (seconds) cells (current cell and the two previous ones) are NaN, else "rolling_nan" = 0.
I am applying a function since the .rolling
pandas function does not support isna()
. However I am getting the following. Also I am not sure how to do include the same row value in the NaN checker.
import pandas as pd
import numpy as np
idx = pd.date_range('2018-01-01', periods=10, freq='S')
df = pd.DataFrame({"A":[1,2,3,np.nan,np.nan,np.nan,6,7,8,9]}, index = idx)
df
def isna_func(x):
return 1 if pd.isna(x).all() == True else 0
df['rolling_nan'] = df['A'].rolling(3).apply(isna_func)
df
A rolling_nan
2018-01-01 00:00:00 1.0 NaN
2018-01-01 00:00:01 2.0 NaN
2018-01-01 00:00:02 3.0 0.0
2018-01-01 00:00:03 NaN NaN
2018-01-01 00:00:04 NaN NaN
2018-01-01 00:00:05 NaN NaN
2018-01-01 00:00:06 6.0 NaN
2018-01-01 00:00:07 7.0 NaN
2018-01-01 00:00:08 8.0 0.0
2018-01-01 00:00:09 9.0 0.0
In the above example, the rolling_nan
should be equal to 1 only at timestamp 2018-01-01 00:00:05
and 0 otherwise.