@@ -55,12 +55,20 @@ def __init__(self, obj, window=None, min_periods=None, freq=None,
5555 self .freq = freq
5656 self .center = center
5757 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 ()
5960
6061 @property
6162 def _constructor (self ):
6263 return Window
6364
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+
6472 def _convert_freq (self , how = None ):
6573 """ resample according to the how, return a new object """
6674
@@ -305,24 +313,62 @@ class Window(_Window):
305313 * ``slepian`` (needs width).
306314"""
307315
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 ()
311318
319+ window = self .window
312320 if isinstance (window , (list , tuple , np .ndarray )):
313- return com . _asarray_tuplesafe ( window ). astype ( float )
321+ pass
314322 elif com .is_integer (window ):
315323 try :
316324 import scipy .signal as sig
317325 except ImportError :
318326 raise ImportError ('Please install scipy to generate window '
319327 '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+
320348 # 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+
321369 win_type = _validate_win_type (self .win_type , kwargs )
322370 return sig .get_window (win_type , window ).astype (float )
323371
324- raise ValueError ('Invalid window %s' % str (window ))
325-
326372 def _apply_window (self , mean = True , how = None , ** kwargs ):
327373 """
328374 Applies a moving window of type ``window_type`` on the data.
@@ -791,6 +837,11 @@ class Rolling(_Rolling_and_Expanding):
791837 of :meth:`~pandas.Series.resample` (i.e. using the `mean`).
792838 """
793839
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+
794845 @Substitution (name = 'rolling' )
795846 @Appender (SelectionMixin ._see_also_template )
796847 @Appender (SelectionMixin ._agg_doc )
@@ -1459,28 +1510,6 @@ def _prep_binary(arg1, arg2):
14591510 return X , Y
14601511
14611512
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-
14841513# Top-level exports
14851514
14861515
0 commit comments