57
57
as a frequency string or DateOffset object.
58
58
center : boolean, default False
59
59
Set the labels at the center of the window.
60
+ how : string, default '%s'
61
+ Method for down- or re-sampling
60
62
"""
61
63
62
64
_roll_notes = r"""
85
87
adjust : boolean, default True
86
88
Divide by decaying adjustment factor in beginning periods to account for
87
89
imbalance in relative weightings (viewing EWMA as a moving average)
90
+ how : string, default 'mean'
91
+ Method for down- or re-sampling
88
92
"""
89
93
90
94
_ewm_notes = r"""
148
152
"""
149
153
150
154
151
- def rolling_count (arg , window , freq = None , center = False ):
155
+ def rolling_count (arg , window , freq = None , center = False , how = None ):
152
156
"""
153
157
Rolling count of number of non-NaN observations inside provided window.
154
158
@@ -163,6 +167,8 @@ def rolling_count(arg, window, freq=None, center=False):
163
167
as a frequency string or DateOffset object.
164
168
center : boolean, default False
165
169
Whether the label should correspond with center of window
170
+ how : string, default 'mean'
171
+ Method for down- or re-sampling
166
172
167
173
Returns
168
174
-------
@@ -174,7 +180,7 @@ def rolling_count(arg, window, freq=None, center=False):
174
180
frequency by resampling the data. This is done with the default parameters
175
181
of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
176
182
"""
177
- arg = _conv_timerule (arg , freq )
183
+ arg = _conv_timerule (arg , freq , how )
178
184
window = min (window , len (arg ))
179
185
180
186
return_hook , values = _process_data_structure (arg , kill_inf = False )
@@ -190,19 +196,19 @@ def rolling_count(arg, window, freq=None, center=False):
190
196
191
197
192
198
@Substitution ("Unbiased moving covariance." , _binary_arg_flex ,
193
- _roll_kw + _pairwise_kw , _flex_retval , _roll_notes )
199
+ _roll_kw % 'None' + _pairwise_kw , _flex_retval , _roll_notes )
194
200
@Appender (_doc_template )
195
201
def rolling_cov (arg1 , arg2 = None , window = None , min_periods = None , freq = None ,
196
- center = False , pairwise = None ):
202
+ center = False , pairwise = None , how = None ):
197
203
if window is None and isinstance (arg2 , (int , float )):
198
204
window = arg2
199
205
arg2 = arg1
200
206
pairwise = True if pairwise is None else pairwise # only default unset
201
207
elif arg2 is None :
202
208
arg2 = arg1
203
209
pairwise = True if pairwise is None else pairwise # only default unset
204
- arg1 = _conv_timerule (arg1 , freq )
205
- arg2 = _conv_timerule (arg2 , freq )
210
+ arg1 = _conv_timerule (arg1 , freq , how )
211
+ arg2 = _conv_timerule (arg2 , freq , how )
206
212
window = min (window , len (arg1 ), len (arg2 ))
207
213
208
214
def _get_cov (X , Y ):
@@ -215,19 +221,19 @@ def _get_cov(X, Y):
215
221
216
222
217
223
@Substitution ("Moving sample correlation." , _binary_arg_flex ,
218
- _roll_kw + _pairwise_kw , _flex_retval , _roll_notes )
224
+ _roll_kw % 'None' + _pairwise_kw , _flex_retval , _roll_notes )
219
225
@Appender (_doc_template )
220
226
def rolling_corr (arg1 , arg2 = None , window = None , min_periods = None , freq = None ,
221
- center = False , pairwise = None ):
227
+ center = False , pairwise = None , how = None ):
222
228
if window is None and isinstance (arg2 , (int , float )):
223
229
window = arg2
224
230
arg2 = arg1
225
231
pairwise = True if pairwise is None else pairwise # only default unset
226
232
elif arg2 is None :
227
233
arg2 = arg1
228
234
pairwise = True if pairwise is None else pairwise # only default unset
229
- arg1 = _conv_timerule (arg1 , freq )
230
- arg2 = _conv_timerule (arg2 , freq )
235
+ arg1 = _conv_timerule (arg1 , freq , how )
236
+ arg2 = _conv_timerule (arg2 , freq , how )
231
237
window = min (window , len (arg1 ), len (arg2 ))
232
238
233
239
def _get_corr (a , b ):
@@ -289,7 +295,7 @@ def _flex_binary_moment(arg1, arg2, f, pairwise=False):
289
295
290
296
@Substitution ("Deprecated. Use rolling_corr(..., pairwise=True) instead.\n \n "
291
297
"Pairwise moving sample correlation" , _pairwise_arg ,
292
- _roll_kw , _pairwise_retval , _roll_notes )
298
+ _roll_kw % 'None' , _pairwise_retval , _roll_notes )
293
299
@Appender (_doc_template )
294
300
def rolling_corr_pairwise (df1 , df2 = None , window = None , min_periods = None ,
295
301
freq = None , center = False ):
@@ -301,7 +307,7 @@ def rolling_corr_pairwise(df1, df2=None, window=None, min_periods=None,
301
307
302
308
303
309
def _rolling_moment (arg , window , func , minp , axis = 0 , freq = None , center = False ,
304
- args = (), kwargs = {}, ** kwds ):
310
+ how = None , args = (), kwargs = {}, ** kwds ):
305
311
"""
306
312
Rolling statistical measure using supplied function. Designed to be
307
313
used with passed-in Cython array-based functions.
@@ -318,6 +324,8 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None, center=False,
318
324
Frequency to conform to before computing statistic
319
325
center : boolean, default False
320
326
Whether the label should correspond with center of window
327
+ how : string, default 'mean'
328
+ Method for down- or re-sampling
321
329
args : tuple
322
330
Passed on to func
323
331
kwargs : dict
@@ -327,7 +335,7 @@ def _rolling_moment(arg, window, func, minp, axis=0, freq=None, center=False,
327
335
-------
328
336
y : type of input
329
337
"""
330
- arg = _conv_timerule (arg , freq )
338
+ arg = _conv_timerule (arg , freq , how )
331
339
calc = lambda x : func (x , window , minp = minp , args = args , kwargs = kwargs ,
332
340
** kwds )
333
341
return_hook , values = _process_data_structure (arg )
@@ -413,9 +421,9 @@ def _get_center_of_mass(com, span, halflife):
413
421
_type_of_input_retval , _ewm_notes )
414
422
@Appender (_doc_template )
415
423
def ewma (arg , com = None , span = None , halflife = None , min_periods = 0 , freq = None ,
416
- adjust = True ):
424
+ adjust = True , how = None ):
417
425
com = _get_center_of_mass (com , span , halflife )
418
- arg = _conv_timerule (arg , freq )
426
+ arg = _conv_timerule (arg , freq , how )
419
427
420
428
def _ewma (v ):
421
429
result = algos .ewma (v , com , int (adjust ))
@@ -437,9 +445,9 @@ def _first_valid_index(arr):
437
445
_ewm_kw + _bias_kw , _type_of_input_retval , _ewm_notes )
438
446
@Appender (_doc_template )
439
447
def ewmvar (arg , com = None , span = None , halflife = None , min_periods = 0 , bias = False ,
440
- freq = None ):
448
+ freq = None , how = None ):
441
449
com = _get_center_of_mass (com , span , halflife )
442
- arg = _conv_timerule (arg , freq )
450
+ arg = _conv_timerule (arg , freq , how )
443
451
moment2nd = ewma (arg * arg , com = com , min_periods = min_periods )
444
452
moment1st = ewma (arg , com = com , min_periods = min_periods )
445
453
@@ -465,16 +473,16 @@ def ewmstd(arg, com=None, span=None, halflife=None, min_periods=0, bias=False):
465
473
_ewm_kw + _pairwise_kw , _type_of_input_retval , _ewm_notes )
466
474
@Appender (_doc_template )
467
475
def ewmcov (arg1 , arg2 = None , com = None , span = None , halflife = None , min_periods = 0 ,
468
- bias = False , freq = None , pairwise = None ):
476
+ bias = False , freq = None , pairwise = None , how = None ):
469
477
if arg2 is None :
470
478
arg2 = arg1
471
479
pairwise = True if pairwise is None else pairwise
472
480
elif isinstance (arg2 , (int , float )) and com is None :
473
481
com = arg2
474
482
arg2 = arg1
475
483
pairwise = True if pairwise is None else pairwise
476
- arg1 = _conv_timerule (arg1 , freq )
477
- arg2 = _conv_timerule (arg2 , freq )
484
+ arg1 = _conv_timerule (arg1 , freq , how )
485
+ arg2 = _conv_timerule (arg2 , freq , how )
478
486
479
487
def _get_ewmcov (X , Y ):
480
488
mean = lambda x : ewma (x , com = com , span = span , halflife = halflife , min_periods = min_periods )
@@ -492,16 +500,16 @@ def _get_ewmcov(X, Y):
492
500
_ewm_kw + _pairwise_kw , _type_of_input_retval , _ewm_notes )
493
501
@Appender (_doc_template )
494
502
def ewmcorr (arg1 , arg2 = None , com = None , span = None , halflife = None , min_periods = 0 ,
495
- freq = None , pairwise = None ):
503
+ freq = None , pairwise = None , how = None ):
496
504
if arg2 is None :
497
505
arg2 = arg1
498
506
pairwise = True if pairwise is None else pairwise
499
507
elif isinstance (arg2 , (int , float )) and com is None :
500
508
com = arg2
501
509
arg2 = arg1
502
510
pairwise = True if pairwise is None else pairwise
503
- arg1 = _conv_timerule (arg1 , freq )
504
- arg2 = _conv_timerule (arg2 , freq )
511
+ arg1 = _conv_timerule (arg1 , freq , how )
512
+ arg2 = _conv_timerule (arg2 , freq , how )
505
513
506
514
def _get_ewmcorr (X , Y ):
507
515
mean = lambda x : ewma (x , com = com , span = span , halflife = halflife , min_periods = min_periods )
@@ -541,12 +549,12 @@ def _prep_binary(arg1, arg2):
541
549
# Python interface to Cython functions
542
550
543
551
544
- def _conv_timerule (arg , freq ):
552
+ def _conv_timerule (arg , freq , how ):
545
553
546
554
types = (DataFrame , Series )
547
555
if freq is not None and isinstance (arg , types ):
548
556
# Conform to whatever frequency needed.
549
- arg = arg .resample (freq )
557
+ arg = arg .resample (freq , how = how )
550
558
551
559
return arg
552
560
@@ -567,25 +575,32 @@ def _use_window(minp, window):
567
575
return minp
568
576
569
577
570
- def _rolling_func (func , desc , check_minp = _use_window ):
571
- @Substitution (desc , _unary_arg , _roll_kw , _type_of_input_retval , _roll_notes )
578
+ def _rolling_func (func , desc , check_minp = _use_window , how = None ):
579
+ if how is None :
580
+ how_arg_str = 'None'
581
+ else :
582
+ how_arg_str = "'%s" % how
583
+
584
+ @Substitution (desc , _unary_arg , _roll_kw % how_arg_str , _type_of_input_retval ,
585
+ _roll_notes )
572
586
@Appender (_doc_template )
573
587
@wraps (func )
574
- def f (arg , window , min_periods = None , freq = None , center = False ,
588
+ def f (arg , window , min_periods = None , freq = None , center = False , how = how ,
575
589
** kwargs ):
576
590
def call_cython (arg , window , minp , args = (), kwargs = {}, ** kwds ):
577
591
minp = check_minp (minp , window )
578
592
return func (arg , window , minp , ** kwds )
579
593
return _rolling_moment (arg , window , call_cython , min_periods , freq = freq ,
580
- center = center , ** kwargs )
594
+ center = center , how = how , ** kwargs )
581
595
582
596
return f
583
597
584
- rolling_max = _rolling_func (algos .roll_max2 , 'Moving maximum.' )
585
- rolling_min = _rolling_func (algos .roll_min2 , 'Moving minimum.' )
598
+ rolling_max = _rolling_func (algos .roll_max2 , 'Moving maximum.' , how = 'max' )
599
+ rolling_min = _rolling_func (algos .roll_min2 , 'Moving minimum.' , how = 'min' )
586
600
rolling_sum = _rolling_func (algos .roll_sum , 'Moving sum.' )
587
601
rolling_mean = _rolling_func (algos .roll_mean , 'Moving mean.' )
588
- rolling_median = _rolling_func (algos .roll_median_cython , 'Moving median.' )
602
+ rolling_median = _rolling_func (algos .roll_median_cython , 'Moving median.' ,
603
+ how = 'median' )
589
604
590
605
_ts_std = lambda * a , ** kw : _zsqrt (algos .roll_var (* a , ** kw ))
591
606
rolling_std = _rolling_func (_ts_std , 'Unbiased moving standard deviation.' ,
@@ -687,7 +702,7 @@ def call_cython(arg, window, minp, args, kwargs):
687
702
688
703
def rolling_window (arg , window = None , win_type = None , min_periods = None ,
689
704
freq = None , center = False , mean = True ,
690
- axis = 0 , ** kwargs ):
705
+ axis = 0 , how = None , ** kwargs ):
691
706
"""
692
707
Applies a moving window of type ``window_type`` and size ``window``
693
708
on the data.
@@ -711,6 +726,8 @@ def rolling_window(arg, window=None, win_type=None, min_periods=None,
711
726
mean : boolean, default True
712
727
If True computes weighted mean, else weighted sum
713
728
axis : {0, 1}, default 0
729
+ how : string, default 'mean'
730
+ Method for down- or re-sampling
714
731
715
732
Returns
716
733
-------
@@ -761,7 +778,7 @@ def rolling_window(arg, window=None, win_type=None, min_periods=None,
761
778
762
779
minp = _use_window (min_periods , len (window ))
763
780
764
- arg = _conv_timerule (arg , freq )
781
+ arg = _conv_timerule (arg , freq , how )
765
782
return_hook , values = _process_data_structure (arg )
766
783
767
784
f = lambda x : algos .roll_window (x , window , minp , avg = mean )
0 commit comments