Skip to content

Commit df5eee6

Browse files
authored
BUG: Fix memory issues in rolling.min/max (#33693)
1 parent 8aa7072 commit df5eee6

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

asv_bench/benchmarks/rolling.py

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

152152

153-
class PeakMemFixed:
154-
def setup(self):
155-
N = 10
156-
arr = 100 * np.random.random(N)
157-
self.roll = pd.Series(arr).rolling(10)
158-
159-
def peakmem_fixed(self):
160-
# GH 25926
161-
# This is to detect memory leaks in rolling operations.
162-
# To save time this is only ran on one method.
163-
# 6000 iterations is enough for most types of leaks to be detected
164-
for x in range(6000):
165-
self.roll.max()
153+
class PeakMemFixedWindowMinMax:
154+
155+
params = ["min", "max"]
156+
157+
def setup(self, operation):
158+
N = int(1e6)
159+
arr = np.random.random(N)
160+
self.roll = pd.Series(arr).rolling(2)
161+
162+
def peakmem_fixed(self, operation):
163+
for x in range(5):
164+
getattr(self.roll, operation)()
166165

167166

168167
class ForwardWindowMethods:

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ Groupby/resample/rolling
698698
- Bug in :meth:`DataFrame.resample` where an ``AmbiguousTimeError`` would be raised when the resulting timezone aware :class:`DatetimeIndex` had a DST transition at midnight (:issue:`25758`)
699699
- Bug in :meth:`DataFrame.groupby` where a ``ValueError`` would be raised when grouping by a categorical column with read-only categories and ``sort=False`` (:issue:`33410`)
700700
- Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`)
701+
- Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`)
701702

702703
Reshaping
703704
^^^^^^^^^

pandas/_libs/window/aggregations.pyx

+7-9
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,8 @@ cdef inline numeric calc_mm(int64_t minp, Py_ssize_t nobs,
971971
return result
972972

973973

974-
def roll_max_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
975-
ndarray[int64_t] end, int64_t minp, int64_t win):
974+
def roll_max_fixed(float64_t[:] values, int64_t[:] start,
975+
int64_t[:] end, int64_t minp, int64_t win):
976976
"""
977977
Moving max of 1d array of any numeric type along axis=0 ignoring NaNs.
978978
@@ -988,7 +988,7 @@ def roll_max_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
988988
make the interval closed on the right, left,
989989
both or neither endpoints
990990
"""
991-
return _roll_min_max_fixed(values, start, end, minp, win, is_max=1)
991+
return _roll_min_max_fixed(values, minp, win, is_max=1)
992992

993993

994994
def roll_max_variable(ndarray[float64_t] values, ndarray[int64_t] start,
@@ -1011,8 +1011,8 @@ def roll_max_variable(ndarray[float64_t] values, ndarray[int64_t] start,
10111011
return _roll_min_max_variable(values, start, end, minp, is_max=1)
10121012

10131013

1014-
def roll_min_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
1015-
ndarray[int64_t] end, int64_t minp, int64_t win):
1014+
def roll_min_fixed(float64_t[:] values, int64_t[:] start,
1015+
int64_t[:] end, int64_t minp, int64_t win):
10161016
"""
10171017
Moving min of 1d array of any numeric type along axis=0 ignoring NaNs.
10181018
@@ -1025,7 +1025,7 @@ def roll_min_fixed(ndarray[float64_t] values, ndarray[int64_t] start,
10251025
index : ndarray, optional
10261026
index for window computation
10271027
"""
1028-
return _roll_min_max_fixed(values, start, end, minp, win, is_max=0)
1028+
return _roll_min_max_fixed(values, minp, win, is_max=0)
10291029

10301030

10311031
def roll_min_variable(ndarray[float64_t] values, ndarray[int64_t] start,
@@ -1112,9 +1112,7 @@ cdef _roll_min_max_variable(ndarray[numeric] values,
11121112
return output
11131113

11141114

1115-
cdef _roll_min_max_fixed(ndarray[numeric] values,
1116-
ndarray[int64_t] starti,
1117-
ndarray[int64_t] endi,
1115+
cdef _roll_min_max_fixed(numeric[:] values,
11181116
int64_t minp,
11191117
int64_t win,
11201118
bint is_max):

0 commit comments

Comments
 (0)