Skip to content

Commit 7353202

Browse files
adamkleinwesm
authored andcommitted
ENH: #1020 implementation. needs tests and adding to API
1 parent f4ad52e commit 7353202

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pandas/src/moments.pyx

+39
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,45 @@ def median(ndarray arr):
7272
return (kth_smallest(arr, n / 2) +
7373
kth_smallest(arr, n / 2 - 1)) / 2
7474

75+
# -------------- Min, Max subsequence
76+
77+
def max_subseq(ndarray[double_t] arr):
78+
cdef:
79+
Py_ssize_t i=0,s=0,e=0,T,n
80+
double m, S
81+
82+
n = len(arr)
83+
84+
if len(arr) == 0:
85+
return (-1,-1,None)
86+
87+
m = arr[0]
88+
S = m
89+
T = 0
90+
91+
for i in range(1, n):
92+
# S = max { S + A[i], A[i] )
93+
if (S > 0):
94+
S = S + arr[i]
95+
else:
96+
S = arr[i]
97+
T = i
98+
if S > m:
99+
s = T
100+
e = i
101+
m = S
102+
103+
return (s, e, m)
104+
105+
def min_subseq(ndarray[double_t] arr):
106+
cdef:
107+
Py_ssize_t s, e
108+
double m
109+
110+
(s, e, m) = max_subseq(-arr)
111+
112+
return (s, e, -m)
113+
75114
#-------------------------------------------------------------------------------
76115
# Rolling sum
77116

0 commit comments

Comments
 (0)