Trying to get the rolling max in presence of NaN's. For some reason, not getting the expected output. Any thoughts!
# Import library
import pandas as pd
# Data
x = pd.Series([1,2,np.nan,4,np.nan])
# Get rolling max
out = x.rolling(window=2).max()
print(out)
print()
# Get rolling max
out = x.rolling(window=2).max(skipna=True) #<-- deprecation warning
print(out)
Output
0 NaN
1 2.0
2 NaN
3 NaN
4 NaN
dtype: float64
0 NaN
1 2.0
2 NaN
3 NaN
4 NaN
dtype: float64
Warning:
FutureWarning: Passing additional kwargs to Rolling.max has no impact on the result and is deprecated. This will raise a TypeError in a future version of pandas.
Need the following output:
0 NaN
1 2.0
2 2.0
3 4.0
4 4.0
dtype: float64
Edited
Thank you for answers below. However, I need it to work in situation such as below as well. Would .rolling().max()
get the desired output?
New data:
x = pd.Series([1,2,np.nan,np.nan,4,np.nan])
Current output
0 NaN
1 2.0
2 NaN
3 NaN
4 NaN
5 NaN
Desired output
0 NaN
1 2.0
2 2.0
3 NaN
4 4.0
5 4.0