@@ -55,12 +55,20 @@ def __init__(self, obj, window=None, min_periods=None, freq=None,
55
55
self .freq = freq
56
56
self .center = center
57
57
self .win_type = win_type
58
- self .axis = axis
58
+ self .axis = obj ._get_axis_number (axis ) if axis is not None else None
59
+ self .validate ()
59
60
60
61
@property
61
62
def _constructor (self ):
62
63
return Window
63
64
65
+ def validate (self ):
66
+ if self .center is not None and not com .is_bool (self .center ):
67
+ raise ValueError ("center must be a boolean" )
68
+ if self .min_periods is not None and not \
69
+ com .is_integer (self .min_periods ):
70
+ raise ValueError ("min_periods must be an integer" )
71
+
64
72
def _convert_freq (self , how = None ):
65
73
""" resample according to the how, return a new object """
66
74
@@ -305,24 +313,62 @@ class Window(_Window):
305
313
* ``slepian`` (needs width).
306
314
"""
307
315
308
- def _prep_window (self , ** kwargs ):
309
- """ provide validation for our window type, return the window """
310
- window = self ._get_window ()
316
+ def validate (self ):
317
+ super (Window , self ).validate ()
311
318
319
+ window = self .window
312
320
if isinstance (window , (list , tuple , np .ndarray )):
313
- return com . _asarray_tuplesafe ( window ). astype ( float )
321
+ pass
314
322
elif com .is_integer (window ):
315
323
try :
316
324
import scipy .signal as sig
317
325
except ImportError :
318
326
raise ImportError ('Please install scipy to generate window '
319
327
'weight' )
328
+
329
+ if not isinstance (self .win_type , compat .string_types ):
330
+ raise ValueError ('Invalid win_type {0}' .format (self .win_type ))
331
+ if getattr (sig , self .win_type , None ) is None :
332
+ raise ValueError ('Invalid win_type {0}' .format (self .win_type ))
333
+ else :
334
+ raise ValueError ('Invalid window {0}' .format (window ))
335
+
336
+ def _prep_window (self , ** kwargs ):
337
+ """
338
+ provide validation for our window type, return the window
339
+ we have already been validated
340
+ """
341
+
342
+ window = self ._get_window ()
343
+ if isinstance (window , (list , tuple , np .ndarray )):
344
+ return com ._asarray_tuplesafe (window ).astype (float )
345
+ elif com .is_integer (window ):
346
+ import scipy .signal as sig
347
+
320
348
# the below may pop from kwargs
349
+ def _validate_win_type (win_type , kwargs ):
350
+ arg_map = {'kaiser' : ['beta' ],
351
+ 'gaussian' : ['std' ],
352
+ 'general_gaussian' : ['power' , 'width' ],
353
+ 'slepian' : ['width' ]}
354
+ if win_type in arg_map :
355
+ return tuple ([win_type ] + _pop_args (win_type ,
356
+ arg_map [win_type ],
357
+ kwargs ))
358
+ return win_type
359
+
360
+ def _pop_args (win_type , arg_names , kwargs ):
361
+ msg = '%s window requires %%s' % win_type
362
+ all_args = []
363
+ for n in arg_names :
364
+ if n not in kwargs :
365
+ raise ValueError (msg % n )
366
+ all_args .append (kwargs .pop (n ))
367
+ return all_args
368
+
321
369
win_type = _validate_win_type (self .win_type , kwargs )
322
370
return sig .get_window (win_type , window ).astype (float )
323
371
324
- raise ValueError ('Invalid window %s' % str (window ))
325
-
326
372
def _apply_window (self , mean = True , how = None , ** kwargs ):
327
373
"""
328
374
Applies a moving window of type ``window_type`` on the data.
@@ -791,6 +837,11 @@ class Rolling(_Rolling_and_Expanding):
791
837
of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
792
838
"""
793
839
840
+ def validate (self ):
841
+ super (Rolling , self ).validate ()
842
+ if not com .is_integer (self .window ):
843
+ raise ValueError ("window must be an integer" )
844
+
794
845
@Substitution (name = 'rolling' )
795
846
@Appender (SelectionMixin ._see_also_template )
796
847
@Appender (SelectionMixin ._agg_doc )
@@ -1459,28 +1510,6 @@ def _prep_binary(arg1, arg2):
1459
1510
return X , Y
1460
1511
1461
1512
1462
- def _validate_win_type (win_type , kwargs ):
1463
- # may pop from kwargs
1464
- arg_map = {'kaiser' : ['beta' ],
1465
- 'gaussian' : ['std' ],
1466
- 'general_gaussian' : ['power' , 'width' ],
1467
- 'slepian' : ['width' ]}
1468
- if win_type in arg_map :
1469
- return tuple ([win_type ] + _pop_args (win_type , arg_map [win_type ],
1470
- kwargs ))
1471
- return win_type
1472
-
1473
-
1474
- def _pop_args (win_type , arg_names , kwargs ):
1475
- msg = '%s window requires %%s' % win_type
1476
- all_args = []
1477
- for n in arg_names :
1478
- if n not in kwargs :
1479
- raise ValueError (msg % n )
1480
- all_args .append (kwargs .pop (n ))
1481
- return all_args
1482
-
1483
-
1484
1513
# Top-level exports
1485
1514
1486
1515
0 commit comments