如果 (i+1) 大于 (i) 对于 window of 4 中的所有 i(以前的读数),则标记观察结果

flagging observations if (i+1) is larger than (i) for all i in a window of 4 (previous readings)

我有像这样的降雨时间序列:

rainfall
0   3.1
1   2
2   0
3   0
4   12
5   0
6   1
7   2
8   3
9   6
10  1
11  2
12  9

我想使用 python pandas 来标记其前 4 个读数满足此条件的观察:对于范围内的每个 i (len(observations))==> i+1 >我

预期的输出是这样的:

rainfall    Flag test
0   3.1 F
1   2   F
2   0   F
3   0   F
4   12  F
5   0   F
6   1   F
7   2   F
8   3   T
9   6   T
10  1   F
11  2   F
12  9   F

它只为第 9 行返回 T,而前 3 行有这种情况。

我想知道是否有人可以帮助我。

使用, then get difference with numpy.diff, compare and last check all Trues per row by numpy.all:

N = 4
x = np.concatenate([[np.nan] * (N-1), df['rainfall'].values])

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
arr = rolling_window(x, N)
print (arr)
[[ nan  nan  nan  3.1]
 [ nan  nan  3.1  2. ]
 [ nan  3.1  2.   0. ]
 [ 3.1  2.   0.   0. ]
 [ 2.   0.   0.  12. ]
 [ 0.   0.  12.   0. ]
 [ 0.  12.   0.   1. ]
 [12.   0.   1.   2. ]
 [ 0.   1.   2.   3. ]
 [ 1.   2.   3.   6. ]
 [ 2.   3.   6.   1. ]
 [ 3.   6.   1.   2. ]
 [ 6.   1.   2.   9. ]]

df['flag'] = (np.diff(arr, axis=1) > 0).all(axis=1)
print (df)
    rainfall   flag
0        3.1  False
1        2.0  False
2        0.0  False
3        0.0  False
4       12.0  False
5        0.0  False
6        1.0  False
7        2.0  False
8        3.0   True
9        6.0   True
10       1.0  False
11       2.0  False
12       9.0  False