24
24
is_integer_dtype ,
25
25
is_list_like ,
26
26
is_scalar ,
27
- is_timedelta64_dtype ,
28
27
needs_i8_conversion ,
29
28
)
30
29
from pandas .core .dtypes .generic import (
43
42
WindowGroupByMixin ,
44
43
_doc_template ,
45
44
_flex_binary_moment ,
46
- _offset ,
47
45
_shared_docs ,
48
- _use_window ,
49
- _zsqrt ,
46
+ calculate_center_offset ,
50
47
calculate_min_periods ,
48
+ get_weighted_roll_func ,
49
+ zsqrt ,
51
50
)
52
51
from pandas .core .window .indexers import (
53
52
BaseIndexer ,
@@ -252,19 +251,6 @@ def __iter__(self):
252
251
url = "https://github.com/pandas-dev/pandas/issues/11704"
253
252
raise NotImplementedError (f"See issue #11704 { url } " )
254
253
255
- def _get_index (self ) -> Optional [np .ndarray ]:
256
- """
257
- Return integer representations as an ndarray if index is frequency.
258
-
259
- Returns
260
- -------
261
- None or ndarray
262
- """
263
-
264
- if self .is_freq_type :
265
- return self ._on .asi8
266
- return None
267
-
268
254
def _prep_values (self , values : Optional [np .ndarray ] = None ) -> np .ndarray :
269
255
"""Convert input to numpy arrays for Cython routines"""
270
256
if values is None :
@@ -305,17 +291,6 @@ def _wrap_result(self, result, block=None, obj=None):
305
291
306
292
if isinstance (result , np .ndarray ):
307
293
308
- # coerce if necessary
309
- if block is not None :
310
- if is_timedelta64_dtype (block .values .dtype ):
311
- # TODO: do we know what result.dtype is at this point?
312
- # i.e. can we just do an astype?
313
- from pandas import to_timedelta
314
-
315
- result = to_timedelta (result .ravel (), unit = "ns" ).values .reshape (
316
- result .shape
317
- )
318
-
319
294
if result .ndim == 1 :
320
295
from pandas import Series
321
296
@@ -384,14 +359,11 @@ def _center_window(self, result, window) -> np.ndarray:
384
359
if self .axis > result .ndim - 1 :
385
360
raise ValueError ("Requested axis is larger then no. of argument dimensions" )
386
361
387
- offset = _offset (window , True )
362
+ offset = calculate_center_offset (window )
388
363
if offset > 0 :
389
- if isinstance (result , (ABCSeries , ABCDataFrame )):
390
- result = result .slice_shift (- offset , axis = self .axis )
391
- else :
392
- lead_indexer = [slice (None )] * result .ndim
393
- lead_indexer [self .axis ] = slice (offset , None )
394
- result = np .copy (result [tuple (lead_indexer )])
364
+ lead_indexer = [slice (None )] * result .ndim
365
+ lead_indexer [self .axis ] = slice (offset , None )
366
+ result = np .copy (result [tuple (lead_indexer )])
395
367
return result
396
368
397
369
def _get_roll_func (self , func_name : str ) -> Callable :
@@ -424,17 +396,15 @@ def _get_cython_func_type(self, func: str) -> Callable:
424
396
return self ._get_roll_func (f"{ func } _variable" )
425
397
return partial (self ._get_roll_func (f"{ func } _fixed" ), win = self ._get_window ())
426
398
427
- def _get_window_indexer (
428
- self , index_as_array : Optional [np .ndarray ], window : int
429
- ) -> BaseIndexer :
399
+ def _get_window_indexer (self , window : int ) -> BaseIndexer :
430
400
"""
431
401
Return an indexer class that will compute the window start and end bounds
432
402
"""
433
403
if isinstance (self .window , BaseIndexer ):
434
404
return self .window
435
405
if self .is_freq_type :
436
- return VariableWindowIndexer (index_array = index_as_array , window_size = window )
437
- return FixedWindowIndexer (index_array = index_as_array , window_size = window )
406
+ return VariableWindowIndexer (index_array = self . _on . asi8 , window_size = window )
407
+ return FixedWindowIndexer (window_size = window )
438
408
439
409
def _apply (
440
410
self ,
@@ -476,8 +446,7 @@ def _apply(
476
446
477
447
blocks , obj = self ._create_blocks ()
478
448
block_list = list (blocks )
479
- index_as_array = self ._get_index ()
480
- window_indexer = self ._get_window_indexer (index_as_array , window )
449
+ window_indexer = self ._get_window_indexer (window )
481
450
482
451
results = []
483
452
exclude : List [Scalar ] = []
@@ -498,7 +467,7 @@ def _apply(
498
467
continue
499
468
500
469
# calculation function
501
- offset = _offset (window , center ) if center else 0
470
+ offset = calculate_center_offset (window ) if center else 0
502
471
additional_nans = np .array ([np .nan ] * offset )
503
472
504
473
if not is_weighted :
@@ -1051,15 +1020,6 @@ def _get_window(
1051
1020
# GH #15662. `False` makes symmetric window, rather than periodic.
1052
1021
return sig .get_window (win_type , window , False ).astype (float )
1053
1022
1054
- def _get_weighted_roll_func (
1055
- self , cfunc : Callable , check_minp : Callable , ** kwargs
1056
- ) -> Callable :
1057
- def func (arg , window , min_periods = None , closed = None ):
1058
- minp = check_minp (min_periods , len (window ))
1059
- return cfunc (arg , window , minp , ** kwargs )
1060
-
1061
- return func
1062
-
1063
1023
_agg_see_also_doc = dedent (
1064
1024
"""
1065
1025
See Also
@@ -1127,7 +1087,7 @@ def aggregate(self, func, *args, **kwargs):
1127
1087
def sum (self , * args , ** kwargs ):
1128
1088
nv .validate_window_func ("sum" , args , kwargs )
1129
1089
window_func = self ._get_roll_func ("roll_weighted_sum" )
1130
- window_func = self . _get_weighted_roll_func (window_func , _use_window )
1090
+ window_func = get_weighted_roll_func (window_func )
1131
1091
return self ._apply (
1132
1092
window_func , center = self .center , is_weighted = True , name = "sum" , ** kwargs
1133
1093
)
@@ -1137,7 +1097,7 @@ def sum(self, *args, **kwargs):
1137
1097
def mean (self , * args , ** kwargs ):
1138
1098
nv .validate_window_func ("mean" , args , kwargs )
1139
1099
window_func = self ._get_roll_func ("roll_weighted_mean" )
1140
- window_func = self . _get_weighted_roll_func (window_func , _use_window )
1100
+ window_func = get_weighted_roll_func (window_func )
1141
1101
return self ._apply (
1142
1102
window_func , center = self .center , is_weighted = True , name = "mean" , ** kwargs
1143
1103
)
@@ -1147,7 +1107,7 @@ def mean(self, *args, **kwargs):
1147
1107
def var (self , ddof = 1 , * args , ** kwargs ):
1148
1108
nv .validate_window_func ("var" , args , kwargs )
1149
1109
window_func = partial (self ._get_roll_func ("roll_weighted_var" ), ddof = ddof )
1150
- window_func = self . _get_weighted_roll_func (window_func , _use_window )
1110
+ window_func = get_weighted_roll_func (window_func )
1151
1111
kwargs .pop ("name" , None )
1152
1112
return self ._apply (
1153
1113
window_func , center = self .center , is_weighted = True , name = "var" , ** kwargs
@@ -1157,7 +1117,7 @@ def var(self, ddof=1, *args, **kwargs):
1157
1117
@Appender (_shared_docs ["std" ])
1158
1118
def std (self , ddof = 1 , * args , ** kwargs ):
1159
1119
nv .validate_window_func ("std" , args , kwargs )
1160
- return _zsqrt (self .var (ddof = ddof , name = "std" , ** kwargs ))
1120
+ return zsqrt (self .var (ddof = ddof , name = "std" , ** kwargs ))
1161
1121
1162
1122
1163
1123
class _Rolling (_Window ):
@@ -1211,8 +1171,6 @@ class _Rolling_and_Expanding(_Rolling):
1211
1171
def count (self ):
1212
1172
1213
1173
blocks , obj = self ._create_blocks ()
1214
- # Validate the index
1215
- self ._get_index ()
1216
1174
1217
1175
window = self ._get_window ()
1218
1176
window = min (window , len (obj )) if not self .center else window
@@ -1307,7 +1265,7 @@ def apply(
1307
1265
kwargs .pop ("_level" , None )
1308
1266
kwargs .pop ("floor" , None )
1309
1267
window = self ._get_window ()
1310
- offset = _offset (window , self .center )
1268
+ offset = calculate_center_offset (window ) if self .center else 0
1311
1269
if not is_bool (raw ):
1312
1270
raise ValueError ("raw parameter must be `True` or `False`" )
1313
1271
@@ -1478,7 +1436,7 @@ def std(self, ddof=1, *args, **kwargs):
1478
1436
window_func = self ._get_cython_func_type ("roll_var" )
1479
1437
1480
1438
def zsqrt_func (values , begin , end , min_periods ):
1481
- return _zsqrt (window_func (values , begin , end , min_periods , ddof = ddof ))
1439
+ return zsqrt (window_func (values , begin , end , min_periods , ddof = ddof ))
1482
1440
1483
1441
# ddof passed again for compat with groupby.rolling
1484
1442
return self ._apply (
0 commit comments