74
74
from pandas .core .internals import Block # noqa:F401
75
75
76
76
77
- def calculate_min_periods (
78
- window : int ,
79
- min_periods : Optional [int ],
80
- num_values : int ,
81
- required_min_periods : int ,
82
- floor : int ,
83
- ) -> int :
84
- """
85
- Calculate final minimum periods value for rolling aggregations.
86
-
87
- Parameters
88
- ----------
89
- window : passed window value
90
- min_periods : passed min periods value
91
- num_values : total number of values
92
- required_min_periods : required min periods per aggregation function
93
- floor : required min periods per aggregation function
94
-
95
- Returns
96
- -------
97
- min_periods : int
98
- """
99
- if min_periods is None :
100
- min_periods = window
101
- else :
102
- min_periods = max (required_min_periods , min_periods )
103
- if min_periods > window :
104
- raise ValueError (f"min_periods { min_periods } must be <= window { window } " )
105
- elif min_periods > num_values :
106
- min_periods = num_values + 1
107
- elif min_periods < 0 :
108
- raise ValueError ("min_periods must be >= 0" )
109
- return max (min_periods , floor )
110
-
111
-
112
77
class BaseWindow (ShallowMixin , SelectionMixin ):
113
78
"""Provides utilities for performing windowing operations."""
114
79
@@ -163,8 +128,15 @@ def is_freq_type(self) -> bool:
163
128
def validate (self ) -> None :
164
129
if self .center is not None and not is_bool (self .center ):
165
130
raise ValueError ("center must be a boolean" )
166
- if self .min_periods is not None and not is_integer (self .min_periods ):
167
- raise ValueError ("min_periods must be an integer" )
131
+ if self .min_periods is not None :
132
+ if not is_integer (self .min_periods ):
133
+ raise ValueError ("min_periods must be an integer" )
134
+ elif self .min_periods < 0 :
135
+ raise ValueError ("min_periods must be >= 0" )
136
+ elif is_integer (self .window ) and self .min_periods > self .window :
137
+ raise ValueError (
138
+ f"min_periods { self .min_periods } must be <= window { self .window } "
139
+ )
168
140
if self .closed is not None and self .closed not in [
169
141
"right" ,
170
142
"both" ,
@@ -433,8 +405,6 @@ def hfunc(bvalues: ArrayLike) -> ArrayLike:
433
405
def _apply (
434
406
self ,
435
407
func : Callable [..., Any ],
436
- require_min_periods : int = 0 ,
437
- floor : int = 1 ,
438
408
name : Optional [str ] = None ,
439
409
use_numba_cache : bool = False ,
440
410
** kwargs ,
@@ -447,8 +417,6 @@ def _apply(
447
417
Parameters
448
418
----------
449
419
func : callable function to apply
450
- require_min_periods : int
451
- floor : int
452
420
name : str,
453
421
use_numba_cache : bool
454
422
whether to cache a numba compiled function. Only available for numba
@@ -462,6 +430,11 @@ def _apply(
462
430
"""
463
431
window = self ._get_window ()
464
432
window_indexer = self ._get_window_indexer (window )
433
+ min_periods = (
434
+ self .min_periods
435
+ if self .min_periods is not None
436
+ else window_indexer .window_size
437
+ )
465
438
466
439
def homogeneous_func (values : np .ndarray ):
467
440
# calculation function
@@ -470,21 +443,9 @@ def homogeneous_func(values: np.ndarray):
470
443
return values .copy ()
471
444
472
445
def calc (x ):
473
- if not isinstance (self .window , BaseIndexer ):
474
- min_periods = calculate_min_periods (
475
- window , self .min_periods , len (x ), require_min_periods , floor
476
- )
477
- else :
478
- min_periods = calculate_min_periods (
479
- window_indexer .window_size ,
480
- self .min_periods ,
481
- len (x ),
482
- require_min_periods ,
483
- floor ,
484
- )
485
446
start , end = window_indexer .get_window_bounds (
486
447
num_values = len (x ),
487
- min_periods = self . min_periods ,
448
+ min_periods = min_periods ,
488
449
center = self .center ,
489
450
closed = self .closed ,
490
451
)
@@ -793,16 +754,12 @@ def __init__(self, obj, *args, **kwargs):
793
754
def _apply (
794
755
self ,
795
756
func : Callable [..., Any ],
796
- require_min_periods : int = 0 ,
797
- floor : int = 1 ,
798
757
name : Optional [str ] = None ,
799
758
use_numba_cache : bool = False ,
800
759
** kwargs ,
801
760
) -> FrameOrSeries :
802
761
result = super ()._apply (
803
762
func ,
804
- require_min_periods ,
805
- floor ,
806
763
name ,
807
764
use_numba_cache ,
808
765
** kwargs ,
@@ -1151,8 +1108,6 @@ def _get_window_weights(
1151
1108
def _apply (
1152
1109
self ,
1153
1110
func : Callable [[np .ndarray , int , int ], np .ndarray ],
1154
- require_min_periods : int = 0 ,
1155
- floor : int = 1 ,
1156
1111
name : Optional [str ] = None ,
1157
1112
use_numba_cache : bool = False ,
1158
1113
** kwargs ,
@@ -1165,8 +1120,6 @@ def _apply(
1165
1120
Parameters
1166
1121
----------
1167
1122
func : callable function to apply
1168
- require_min_periods : int
1169
- floor : int
1170
1123
name : str,
1171
1124
use_numba_cache : bool
1172
1125
whether to cache a numba compiled function. Only available for numba
@@ -1420,7 +1373,6 @@ def apply(
1420
1373
1421
1374
return self ._apply (
1422
1375
apply_func ,
1423
- floor = 0 ,
1424
1376
use_numba_cache = maybe_use_numba (engine ),
1425
1377
original_func = func ,
1426
1378
args = args ,
@@ -1454,7 +1406,7 @@ def apply_func(values, begin, end, min_periods, raw=raw):
1454
1406
def sum (self , * args , ** kwargs ):
1455
1407
nv .validate_window_func ("sum" , args , kwargs )
1456
1408
window_func = self ._get_roll_func ("roll_sum" )
1457
- return self ._apply (window_func , floor = 0 , name = "sum" , ** kwargs )
1409
+ return self ._apply (window_func , name = "sum" , ** kwargs )
1458
1410
1459
1411
_shared_docs ["max" ] = dedent (
1460
1412
"""
@@ -1571,7 +1523,6 @@ def zsqrt_func(values, begin, end, min_periods):
1571
1523
1572
1524
return self ._apply (
1573
1525
zsqrt_func ,
1574
- require_min_periods = 1 ,
1575
1526
name = "std" ,
1576
1527
** kwargs ,
1577
1528
)
@@ -1581,7 +1532,6 @@ def var(self, ddof: int = 1, *args, **kwargs):
1581
1532
window_func = partial (self ._get_roll_func ("roll_var" ), ddof = ddof )
1582
1533
return self ._apply (
1583
1534
window_func ,
1584
- require_min_periods = 1 ,
1585
1535
name = "var" ,
1586
1536
** kwargs ,
1587
1537
)
@@ -1601,7 +1551,6 @@ def skew(self, **kwargs):
1601
1551
window_func = self ._get_roll_func ("roll_skew" )
1602
1552
return self ._apply (
1603
1553
window_func ,
1604
- require_min_periods = 3 ,
1605
1554
name = "skew" ,
1606
1555
** kwargs ,
1607
1556
)
@@ -1695,7 +1644,6 @@ def kurt(self, **kwargs):
1695
1644
window_func = self ._get_roll_func ("roll_kurt" )
1696
1645
return self ._apply (
1697
1646
window_func ,
1698
- require_min_periods = 4 ,
1699
1647
name = "kurt" ,
1700
1648
** kwargs ,
1701
1649
)
0 commit comments