Pandas rolling max by skipping NaN

问题描述 投票:0回答:2

尝试在 NaN 存在的情况下获得滚动最大值。由于某种原因,没有得到预期的输出。任何想法!

# 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)

输出

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

警告:

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.

需要以下输出:

0    NaN
1    2.0
2    2.0
3    4.0
4    4.0
dtype: float64

已编辑

感谢您在下面的回答。但是,我也需要它在下面的情况下工作。

.rolling().max()
会得到想要的输出吗?

新数据:

x = pd.Series([1,2,np.nan,np.nan,4,np.nan])

电流输出

0    NaN
1    2.0
2    NaN
3    NaN
4    NaN
5    NaN

期望的输出

0    NaN
1    2.0
2    2.0
3    NaN
4    4.0
5    4.0
python pandas max rolling-computation skip
2个回答
1
投票

你可以向前填充,因为它不会改变

rolling_max
的结果:

>>> x.ffill().rolling(window=2).max()
0    NaN
1    2.0
2    2.0
3    4.0
4    4.0
dtype: float64

0
投票

例子

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

代码

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

输出:

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

为了比较,我将 x 和滚动结果制作成数据框。

    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
© www.soinside.com 2019 - 2024. All rights reserved.