@@ -31,8 +31,6 @@ def sliding_min_max(
31
31
nobs = 0
32
32
output = np .empty (N , dtype = result_dtype )
33
33
na_pos = []
34
- # Use deque once numba supports it
35
- # https://github.com/numba/numba/issues/7417
36
34
Q : list = []
37
35
W : list = []
38
36
for i in range (N ):
@@ -46,36 +44,28 @@ def sliding_min_max(
46
44
ai = values [k ]
47
45
if not np .isnan (ai ):
48
46
nobs += 1
49
- elif is_max :
50
- ai = - np .inf
51
47
else :
52
- ai = np .inf
53
- # Discard previous entries if we find new min or max
48
+ ai = - np .inf if is_max else np .inf
54
49
if is_max :
55
- while Q and ((ai >= values [Q [- 1 ]]) or values [Q [- 1 ]] != values [ Q [ - 1 ]] ):
50
+ while Q and ((ai >= values [Q [- 1 ]]) or np . isnan ( values [Q [- 1 ]]) ):
56
51
Q .pop ()
57
52
else :
58
- while Q and ((ai <= values [Q [- 1 ]]) or values [Q [- 1 ]] != values [ Q [ - 1 ]] ):
53
+ while Q and ((ai <= values [Q [- 1 ]]) or np . isnan ( values [Q [- 1 ]]) ):
59
54
Q .pop ()
60
55
Q .append (k )
61
56
W .append (k )
62
57
63
- # Discard entries outside and left of current window
64
58
while Q and Q [0 ] <= start [i ] - 1 :
65
59
Q .pop (0 )
66
60
while W and W [0 ] <= start [i ] - 1 :
67
61
if not np .isnan (values [W [0 ]]):
68
62
nobs -= 1
69
63
W .pop (0 )
70
64
71
- # Save output based on index in input value array
72
65
if Q and curr_win_size > 0 and nobs >= min_periods :
73
66
output [i ] = values [Q [0 ]]
74
67
else :
75
- if values .dtype .kind != "i" :
76
- output [i ] = np .nan
77
- else :
78
- na_pos .append (i )
68
+ output [i ] = np .nan
79
69
80
70
return output , na_pos
81
71
@@ -100,27 +90,31 @@ def grouped_min_max(
100
90
if lab < 0 :
101
91
continue
102
92
103
- if values . dtype . kind == "i" or not np .isnan (val ):
93
+ if not np .isnan (val ):
104
94
nobs [lab ] += 1
105
95
else :
106
- # NaN value cannot be a min/max value
107
96
continue
108
97
109
98
if nobs [lab ] == 1 :
110
- # First element in group, set output equal to this
111
99
output [lab ] = val
112
- continue
113
-
114
- if is_max :
115
- if val > output [lab ]:
116
- output [lab ] = val
117
100
else :
118
- if val < output [lab ]:
119
- output [lab ] = val
101
+ if is_max :
102
+ if val > output [lab ]:
103
+ output [lab ] = val
104
+ else :
105
+ if val < output [lab ]:
106
+ output [lab ] = val
120
107
121
- # Set labels that don't satisfy min_periods as np.nan
122
108
for lab , count in enumerate (nobs ):
123
109
if count < min_periods :
124
- na_pos . append ( lab )
110
+ output [ lab ] = np . nan
125
111
126
112
return output , na_pos
113
+
114
+ # Example usage:
115
+ if __name__ == "__main__" :
116
+ import pandas as pd
117
+
118
+ s = pd .Series ([np .nan , 0 , 1 ], dtype = "Float64" )
119
+ print ((s / s ).max ()) # <NA>
120
+ print ((s / s ).groupby ([9 , 9 , 9 ]).max ().iat [0 ]) # <NA>
0 commit comments