5
5
from collections import defaultdict
6
6
from datetime import timedelta
7
7
from textwrap import dedent
8
- from typing import Set
8
+ from typing import List , Optional , Set
9
9
import warnings
10
10
11
11
import numpy as np
35
35
ABCTimedeltaIndex ,
36
36
)
37
37
38
+ from pandas ._typing import Axis , FrameOrSeries
38
39
from pandas .core .base import DataError , PandasObject , SelectionMixin
39
40
import pandas .core .common as com
40
41
from pandas .core .generic import _shared_docs
@@ -63,24 +64,23 @@ class _Window(PandasObject, SelectionMixin):
63
64
"axis" ,
64
65
"on" ,
65
66
"closed" ,
66
- ]
67
+ ] # type: List[str]
67
68
exclusions = set () # type: Set[str]
68
69
69
70
def __init__ (
70
71
self ,
71
72
obj ,
72
73
window = None ,
73
- min_periods = None ,
74
- center = False ,
75
- win_type = None ,
76
- axis = 0 ,
77
- on = None ,
78
- closed = None ,
74
+ min_periods : Optional [ int ] = None ,
75
+ center : Optional [ bool ] = False ,
76
+ win_type : Optional [ str ] = None ,
77
+ axis : Axis = 0 ,
78
+ on : Optional [ str ] = None ,
79
+ closed : Optional [ str ] = None ,
79
80
** kwargs
80
81
):
81
82
82
83
self .__dict__ .update (kwargs )
83
- self .blocks = []
84
84
self .obj = obj
85
85
self .on = on
86
86
self .closed = closed
@@ -97,15 +97,15 @@ def _constructor(self):
97
97
return Window
98
98
99
99
@property
100
- def is_datetimelike (self ):
100
+ def is_datetimelike (self ) -> Optional [ bool ] :
101
101
return None
102
102
103
103
@property
104
104
def _on (self ):
105
105
return None
106
106
107
107
@property
108
- def is_freq_type (self ):
108
+ def is_freq_type (self ) -> bool :
109
109
return self .win_type == "freq"
110
110
111
111
def validate (self ):
@@ -121,30 +121,20 @@ def validate(self):
121
121
]:
122
122
raise ValueError ("closed must be 'right', 'left', 'both' or " "'neither'" )
123
123
124
- def _convert_freq (self ):
125
- """
126
- Resample according to the how, return a new object.
127
- """
128
- obj = self ._selected_obj
129
- index = None
130
- return obj , index
131
-
132
124
def _create_blocks (self ):
133
125
"""
134
126
Split data into blocks & return conformed data.
135
127
"""
136
128
137
- obj , index = self ._convert_freq ()
138
- if index is not None :
139
- index = self ._on
129
+ obj = self ._selected_obj
140
130
141
131
# filter out the on from the object
142
132
if self .on is not None :
143
133
if obj .ndim == 2 :
144
134
obj = obj .reindex (columns = obj .columns .difference ([self .on ]), copy = False )
145
135
blocks = obj ._to_dict_of_blocks (copy = False ).values ()
146
136
147
- return blocks , obj , index
137
+ return blocks , obj
148
138
149
139
def _gotitem (self , key , ndim , subset = None ):
150
140
"""
@@ -186,10 +176,10 @@ def _get_window(self, other=None):
186
176
return self .window
187
177
188
178
@property
189
- def _window_type (self ):
179
+ def _window_type (self ) -> str :
190
180
return self .__class__ .__name__
191
181
192
- def __repr__ (self ):
182
+ def __repr__ (self ) -> str :
193
183
"""
194
184
Provide a nice str repr of our rolling object.
195
185
"""
@@ -207,23 +197,21 @@ def __iter__(self):
207
197
url = "https://github.com/pandas-dev/pandas/issues/11704"
208
198
raise NotImplementedError ("See issue #11704 {url}" .format (url = url ))
209
199
210
- def _get_index (self , index = None ) :
200
+ def _get_index (self ) -> Optional [ np . ndarray ] :
211
201
"""
212
- Return index as ndarrays .
202
+ Return index as an ndarray .
213
203
214
204
Returns
215
205
-------
216
- tuple of (index, index_as_ndarray)
206
+ None or ndarray
217
207
"""
218
208
219
209
if self .is_freq_type :
220
- if index is None :
221
- index = self ._on
222
- return index , index .asi8
223
- return index , index
224
-
225
- def _prep_values (self , values = None , kill_inf = True ):
210
+ return self ._on .asi8
211
+ return None
226
212
213
+ def _prep_values (self , values : Optional [np .ndarray ] = None ) -> np .ndarray :
214
+ """Convert input to numpy arrays for Cython routines"""
227
215
if values is None :
228
216
values = getattr (self ._selected_obj , "values" , self ._selected_obj )
229
217
@@ -247,13 +235,12 @@ def _prep_values(self, values=None, kill_inf=True):
247
235
"cannot handle this type -> {0}" "" .format (values .dtype )
248
236
)
249
237
250
- if kill_inf :
251
- values = values .copy ()
252
- values [np .isinf (values )] = np .NaN
238
+ # Always convert inf to nan
239
+ values [np .isinf (values )] = np .NaN
253
240
254
241
return values
255
242
256
- def _wrap_result (self , result , block = None , obj = None ):
243
+ def _wrap_result (self , result , block = None , obj = None ) -> FrameOrSeries :
257
244
"""
258
245
Wrap a single result.
259
246
"""
@@ -281,7 +268,7 @@ def _wrap_result(self, result, block=None, obj=None):
281
268
return type (obj )(result , index = index , columns = block .columns )
282
269
return result
283
270
284
- def _wrap_results (self , results , blocks , obj , exclude = None ):
271
+ def _wrap_results (self , results , blocks , obj , exclude = None ) -> FrameOrSeries :
285
272
"""
286
273
Wrap the results.
287
274
@@ -335,7 +322,7 @@ def _wrap_results(self, results, blocks, obj, exclude=None):
335
322
return obj .astype ("float64" )
336
323
return concat (final , axis = 1 ).reindex (columns = columns , copy = False )
337
324
338
- def _center_window (self , result , window ):
325
+ def _center_window (self , result , window ) -> np . ndarray :
339
326
"""
340
327
Center the result in the window.
341
328
"""
@@ -724,7 +711,7 @@ def _apply_window(self, mean=True, **kwargs):
724
711
window = self ._prep_window (** kwargs )
725
712
center = self .center
726
713
727
- blocks , obj , index = self ._create_blocks ()
714
+ blocks , obj = self ._create_blocks ()
728
715
block_list = list (blocks )
729
716
730
717
results = []
@@ -912,9 +899,9 @@ def _apply(
912
899
if check_minp is None :
913
900
check_minp = _use_window
914
901
915
- blocks , obj , index = self ._create_blocks ()
902
+ blocks , obj = self ._create_blocks ()
916
903
block_list = list (blocks )
917
- index , indexi = self ._get_index (index = index )
904
+ index_as_array = self ._get_index ()
918
905
919
906
results = []
920
907
exclude = []
@@ -947,7 +934,7 @@ def func(arg, window, min_periods=None, closed=None):
947
934
minp = check_minp (min_periods , window )
948
935
# ensure we are only rolling on floats
949
936
arg = ensure_float64 (arg )
950
- return cfunc (arg , window , minp , indexi , closed , ** kwargs )
937
+ return cfunc (arg , window , minp , index_as_array , closed , ** kwargs )
951
938
952
939
# calculation function
953
940
if center :
@@ -1027,9 +1014,9 @@ class _Rolling_and_Expanding(_Rolling):
1027
1014
1028
1015
def count (self ):
1029
1016
1030
- blocks , obj , index = self ._create_blocks ()
1017
+ blocks , obj = self ._create_blocks ()
1031
1018
# Validate the index
1032
- self ._get_index (index = index )
1019
+ self ._get_index ()
1033
1020
1034
1021
window = self ._get_window ()
1035
1022
window = min (window , len (obj )) if not self .center else window
@@ -1088,11 +1075,10 @@ def count(self):
1088
1075
def apply (self , func , raw = None , args = (), kwargs = {}):
1089
1076
from pandas import Series
1090
1077
1091
- # TODO: _level is unused?
1092
- _level = kwargs .pop ("_level" , None ) # noqa
1078
+ kwargs .pop ("_level" , None )
1093
1079
window = self ._get_window ()
1094
1080
offset = _offset (window , self .center )
1095
- index , indexi = self ._get_index ()
1081
+ index_as_array = self ._get_index ()
1096
1082
1097
1083
# TODO: default is for backward compat
1098
1084
# change to False in the future
@@ -1113,7 +1099,16 @@ def f(arg, window, min_periods, closed):
1113
1099
if not raw :
1114
1100
arg = Series (arg , index = self .obj .index )
1115
1101
return libwindow .roll_generic (
1116
- arg , window , minp , indexi , closed , offset , func , raw , args , kwargs
1102
+ arg ,
1103
+ window ,
1104
+ minp ,
1105
+ index_as_array ,
1106
+ closed ,
1107
+ offset ,
1108
+ func ,
1109
+ raw ,
1110
+ args ,
1111
+ kwargs ,
1117
1112
)
1118
1113
1119
1114
return self ._apply (f , func , args = args , kwargs = kwargs , center = False , raw = raw )
@@ -1285,12 +1280,12 @@ def median(self, **kwargs):
1285
1280
def std (self , ddof = 1 , * args , ** kwargs ):
1286
1281
nv .validate_window_func ("std" , args , kwargs )
1287
1282
window = self ._get_window ()
1288
- index , indexi = self ._get_index ()
1283
+ index_as_array = self ._get_index ()
1289
1284
1290
1285
def f (arg , * args , ** kwargs ):
1291
1286
minp = _require_min_periods (1 )(self .min_periods , window )
1292
1287
return _zsqrt (
1293
- libwindow .roll_var (arg , window , minp , indexi , self .closed , ddof )
1288
+ libwindow .roll_var (arg , window , minp , index_as_array , self .closed , ddof )
1294
1289
)
1295
1290
1296
1291
return self ._apply (
@@ -1474,17 +1469,27 @@ def kurt(self, **kwargs):
1474
1469
1475
1470
def quantile (self , quantile , interpolation = "linear" , ** kwargs ):
1476
1471
window = self ._get_window ()
1477
- index , indexi = self ._get_index ()
1472
+ index_as_array = self ._get_index ()
1478
1473
1479
1474
def f (arg , * args , ** kwargs ):
1480
1475
minp = _use_window (self .min_periods , window )
1481
1476
if quantile == 1.0 :
1482
- return libwindow .roll_max (arg , window , minp , indexi , self .closed )
1477
+ return libwindow .roll_max (
1478
+ arg , window , minp , index_as_array , self .closed
1479
+ )
1483
1480
elif quantile == 0.0 :
1484
- return libwindow .roll_min (arg , window , minp , indexi , self .closed )
1481
+ return libwindow .roll_min (
1482
+ arg , window , minp , index_as_array , self .closed
1483
+ )
1485
1484
else :
1486
1485
return libwindow .roll_quantile (
1487
- arg , window , minp , indexi , self .closed , quantile , interpolation
1486
+ arg ,
1487
+ window ,
1488
+ minp ,
1489
+ index_as_array ,
1490
+ self .closed ,
1491
+ quantile ,
1492
+ interpolation ,
1488
1493
)
1489
1494
1490
1495
return self ._apply (f , "quantile" , quantile = quantile , ** kwargs )
@@ -2450,7 +2455,7 @@ def _apply(self, func, **kwargs):
2450
2455
-------
2451
2456
y : same type as input argument
2452
2457
"""
2453
- blocks , obj , index = self ._create_blocks ()
2458
+ blocks , obj = self ._create_blocks ()
2454
2459
block_list = list (blocks )
2455
2460
2456
2461
results = []
0 commit comments