@@ -308,8 +308,28 @@ def apply_minp(arr, ddof, minp):
308
308
gen_hpat_pandas_series_rolling_ddof_impl (arr_var ))
309
309
310
310
311
- def gen_sdc_pandas_series_rolling_impl (window_cls ):
312
- """Generate series rolling methods implementations based on window class"""
311
+ @sdc_register_jitable
312
+ def pop_sum (value , nfinite , result ):
313
+ """Calculate the window sum without old value."""
314
+ if numpy .isfinite (value ):
315
+ nfinite -= 1
316
+ result -= value
317
+
318
+ return nfinite , result
319
+
320
+
321
+ @sdc_register_jitable
322
+ def put_sum (value , nfinite , result ):
323
+ """Calculate the window sum with new value."""
324
+ if numpy .isfinite (value ):
325
+ nfinite += 1
326
+ result += value
327
+
328
+ return nfinite , result
329
+
330
+
331
+ def gen_sdc_pandas_series_rolling_impl (pop , put , init_result = numpy .nan ):
332
+ """Generate series rolling methods implementations based on pop/put funcs"""
313
333
def impl (self ):
314
334
win = self ._window
315
335
minp = self ._min_periods
@@ -322,19 +342,37 @@ def impl(self):
322
342
chunks = parallel_chunks (length )
323
343
for i in prange (len (chunks )):
324
344
chunk = chunks [i ]
325
- window = window_cls (win , minp )
345
+ nroll = 0
346
+ nfinite = 0
347
+ result = init_result
326
348
for idx in range (chunk .start , chunk .stop ):
327
- window .roll (input_arr , idx )
328
- output_arr [idx ] = window .result
329
- window .free ()
349
+ if nroll == 0 :
350
+ start = max (idx + 1 - win , 0 )
351
+ for j in range (start , idx ):
352
+ value = input_arr [j ]
353
+ nfinite , result = put (value , nfinite , result )
354
+ nroll += 1
355
+
356
+ if nroll >= win :
357
+ value = input_arr [idx - win ]
358
+ nfinite , result = pop (value , nfinite , result )
359
+
360
+ value = input_arr [idx ]
361
+ nfinite , result = put (value , nfinite , result )
362
+ nroll += 1
363
+
364
+ if nfinite < minp :
365
+ output_arr [idx ] = numpy .nan
366
+ else :
367
+ output_arr [idx ] = result
330
368
331
369
return pandas .Series (output_arr , input_series ._index ,
332
370
name = input_series ._name )
333
371
return impl
334
372
335
373
336
- sdc_pandas_rolling_series_sum_impl = register_jitable (
337
- gen_sdc_pandas_series_rolling_impl (WindowSum ))
374
+ sdc_pandas_series_rolling_sum_impl = register_jitable (
375
+ gen_sdc_pandas_series_rolling_impl (pop_sum , put_sum , init_result = 0. ))
338
376
339
377
340
378
@sdc_rolling_overload (SeriesRollingType , 'apply' )
@@ -648,7 +686,7 @@ def hpat_pandas_series_rolling_sum(self):
648
686
ty_checker = TypeChecker ('Method rolling.sum().' )
649
687
ty_checker .check (self , SeriesRollingType )
650
688
651
- return sdc_pandas_rolling_series_sum_impl
689
+ return sdc_pandas_series_rolling_sum_impl
652
690
653
691
654
692
@sdc_rolling_overload (SeriesRollingType , 'var' )
0 commit comments