0

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
Nilesh Ingle
  • 1,777
  • 11
  • 17
  • Does this answer your question? [Rolling max excluding current observation in Pandas 1.0](https://stackoverflow.com/questions/61649484/rolling-max-excluding-current-observation-in-pandas-1-0) – bonCodigo May 08 '23 at 05:44

2 Answers2

1

You can fill forward because it doesn't change the result of rolling_max:

>>> x.ffill().rolling(window=2).max()
0    NaN
1    2.0
2    2.0
3    4.0
4    4.0
dtype: float64
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Example

x = pd.Series([1, 2, 6, 5, None, 7, 4, None, None, 3])

x

0    1.0
1    2.0
2    6.0
3    5.0
4    NaN
5    7.0
6    4.0
7    NaN
8    NaN
9    3.0

Code

x.expanding(2).agg(lambda x: x[x.notna()].tail(2).max())

output:

0    NaN
1    2.0
2    6.0
3    6.0
4    6.0
5    7.0
6    7.0
7    7.0
8    7.0
9    4.0

For comparison, I made x and rolling results into dataframe.

    x   rolling
0   1.0 NaN
1   2.0 2.0
2   6.0 6.0
3   5.0 6.0
4   NaN 6.0
5   7.0 7.0
6   4.0 7.0
7   NaN 7.0
8   NaN 7.0
9   3.0 4.0
Panda Kim
  • 6,246
  • 2
  • 12