Skip to content

Commit 84f5b4e

Browse files
Backport PR #33693 on branch 1.0.x (BUG: Fix memory issues in rolling.min/max) (#34027)
1 parent 217413a commit 84f5b4e

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

asv_bench/benchmarks/rolling.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,18 @@ def time_quantile(self, constructor, window, dtype, percentile, interpolation):
129129
self.roll.quantile(percentile, interpolation=interpolation)
130130

131131

132-
class PeakMemFixed:
133-
def setup(self):
134-
N = 10
135-
arr = 100 * np.random.random(N)
136-
self.roll = pd.Series(arr).rolling(10)
137-
138-
def peakmem_fixed(self):
139-
# GH 25926
140-
# This is to detect memory leaks in rolling operations.
141-
# To save time this is only ran on one method.
142-
# 6000 iterations is enough for most types of leaks to be detected
143-
for x in range(6000):
144-
self.roll.max()
132+
class PeakMemFixedWindowMinMax:
133+
134+
params = ["min", "max"]
135+
136+
def setup(self, operation):
137+
N = int(1e6)
138+
arr = np.random.random(N)
139+
self.roll = pd.Series(arr).rolling(2)
140+
141+
def peakmem_fixed(self, operation):
142+
for x in range(5):
143+
getattr(self.roll, operation)()
145144

146145

147146
from .pandas_vb_common import setup # noqa: F401 isort:skip

doc/source/whatsnew/v1.0.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Fixed regressions
2828

2929
Bug fixes
3030
~~~~~~~~~
31-
-
31+
- Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`)
3232
-
3333

3434
Contributors

pandas/_libs/window/aggregations.pyx

+7-9
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,8 @@ cdef inline numeric calc_mm(int64_t minp, Py_ssize_t nobs,
965965
return result
966966

967967

968-
def roll_max_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
969-
ndarray[int64_t] end, int64_t minp, int64_t win):
968+
def roll_max_fixed(float64_t[:] values, int64_t[:] start,
969+
int64_t[:] end, int64_t minp, int64_t win):
970970
"""
971971
Moving max of 1d array of any numeric type along axis=0 ignoring NaNs.
972972
@@ -982,7 +982,7 @@ def roll_max_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
982982
make the interval closed on the right, left,
983983
both or neither endpoints
984984
"""
985-
return _roll_min_max_fixed(values, start, end, minp, win, is_max=1)
985+
return _roll_min_max_fixed(values, minp, win, is_max=1)
986986

987987

988988
def roll_max_variable(ndarray[float64_t] values, ndarray[int64_t] start,
@@ -1005,8 +1005,8 @@ def roll_max_variable(ndarray[float64_t] values, ndarray[int64_t] start,
10051005
return _roll_min_max_variable(values, start, end, minp, is_max=1)
10061006

10071007

1008-
def roll_min_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
1009-
ndarray[int64_t] end, int64_t minp, int64_t win):
1008+
def roll_min_fixed(float64_t[:] values, int64_t[:] start,
1009+
int64_t[:] end, int64_t minp, int64_t win):
10101010
"""
10111011
Moving min of 1d array of any numeric type along axis=0 ignoring NaNs.
10121012
@@ -1019,7 +1019,7 @@ def roll_min_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
10191019
index : ndarray, optional
10201020
index for window computation
10211021
"""
1022-
return _roll_min_max_fixed(values, start, end, minp, win, is_max=0)
1022+
return _roll_min_max_fixed(values, minp, win, is_max=0)
10231023

10241024

10251025
def roll_min_variable(ndarray[float64_t] values, ndarray[int64_t] start,
@@ -1121,9 +1121,7 @@ cdef _roll_min_max_variable(ndarray[numeric] values,
11211121
return output
11221122

11231123

1124-
cdef _roll_min_max_fixed(ndarray[numeric] values,
1125-
ndarray[int64_t] starti,
1126-
ndarray[int64_t] endi,
1124+
cdef _roll_min_max_fixed(numeric[:] values,
11271125
int64_t minp,
11281126
int64_t win,
11291127
bint is_max):

0 commit comments

Comments
 (0)