【问题标题】:pandas DataFrame rolling apply np.argmin and manual np.argmin give different resultspandas DataFrame rolling apply np.argmin 和 manual np.argmin 给出不同的结果
【发布时间】:2021-04-08 12:21:33
【问题描述】:

编辑:简化为更简单的情况

In [1]: np.argmin(pd.Series([-6.0, 7.0, np.NaN]))
Out[2]: 0

In [2]: pd.Series([-6.0, 7.0, np.NaN]).rolling(3).apply(np.argmin)                                                                                                                                                                                                               
Out[2]: 
0   NaN
1   NaN
2   NaN
dtype: float64

In [3]: pd.Series([-6.0, 7.0, np.NaN]).rolling(3).apply(np.argmin)[2]                                                                                                                                                                                                            
Out[3]: nan

为什么这两种计算会给出不同的结果?

原案

试图改进my solution for rolling idxmin/max,我遇到了以下问题。


In [1]: index = map(chr, range(ord('a'), ord('a') + 10))

In [2]: df = pd.DataFrame((10 * np.random.randn(10, 3)).astype(int), index=index)

In [3]: df[0][3:4] = np.NaN

In [4]: df                                                                                                                                                                                                                                                                       
Out[4]: 
      0   1   2
a   0.0  -2  -7
b  -6.0   7   7
c   7.0 -23 -13
d   NaN   4  -6
e   7.0  19  10
f  -3.0   4  -2
g   9.0 -16  -2
h  13.0  15  -2
i   6.0   8   0
j  -9.0 -10  11

In [5]: df.rolling(3).apply(np.argmin)                                                                                                                                                                                                                                           
Out[5]: 
     0    1    2
a  NaN  NaN  NaN
b  NaN  NaN  NaN
c  1.0  2.0  2.0
d  NaN  1.0  1.0
e  NaN  0.0  0.0
f  NaN  0.0  0.0
g  1.0  2.0  1.0
h  0.0  1.0  0.0
i  2.0  0.0  0.0
j  2.0  2.0  0.0

In [6]: np.argmin(pd.Series([-6.0, 7.0, np.NaN]))  # for index 'd', col 0                                                                                                                                                                                                                                                                                                                                                                                                                                                              
Out[6]: 0

np.argmin(对于索引'd',第 0 列)的手动应用程序是否应该与相应的滚动应用程序给出相同的结果?为什么滚动应用程序给我NaN而不是0

【问题讨论】:

    标签: python pandas dataframe numpy rolling-computation


    【解决方案1】:

    当然,这是一个RTFM案例……

    对于 DataFrame.rolling:

    min_periods (int, default None):窗口中需要有值的最小观察次数(否则结果为 NA)。对于由偏移量指定的窗口,min_periods 将默认为 1。否则,min_periods 将默认为窗口的大小。

    所以:

    In [1]: np.argmin(pd.Series([-6.0, 7.0, np.NaN]))                                                                                                                                                                                                                                
    Out[1]: 0
    
    In [2]: pd.Series([-6.0, 7.0, np.NaN]).rolling(3, min_periods=0).apply(np.argmin)[2]                                                                                                                                                                                             
    Out[2]: 0.0
    
    

    【讨论】:

      猜你喜欢
      • 2017-07-16
      • 2022-10-05
      • 1970-01-01
      • 2021-10-07
      • 2021-05-18
      • 1970-01-01
      • 2020-03-19
      • 2018-05-07
      • 1970-01-01
      相关资源
      最近更新 更多