<s> from __future__ import absolute_import , division , print_function <EOL> from distutils . version import LooseVersion <EOL> import pandas as pd <EOL> import numpy as np <EOL> from . . core import DataFrame , Series <EOL> from ... base import tokenize <EOL> def getnanos ( rule ) : <EOL> try : <EOL> return getattr ( rule , '<STR_LIT>' , None ) <EOL> except ValueError : <EOL> return None <EOL> if LooseVersion ( pd . __version__ ) >= '<STR_LIT>' : <EOL> def _resample_apply ( s , rule , how , resample_kwargs ) : <EOL> return getattr ( s . resample ( rule , ** resample_kwargs ) , how ) ( ) <EOL> def _resample ( obj , rule , how , ** kwargs ) : <EOL> resampler = Resampler ( obj , rule , ** kwargs ) <EOL> if how is not None : <EOL> raise FutureWarning ( ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( how ) ) <EOL> return getattr ( resampler , how ) ( ) <EOL> return resampler <EOL> else : <EOL> def _resample_apply ( s , rule , how , resample_kwargs ) : <EOL> return s . resample ( rule , how = how , ** resample_kwargs ) <EOL> def _resample ( obj , rule , how , ** kwargs ) : <EOL> how = how or '<STR_LIT>' <EOL> return getattr ( Resampler ( obj , rule , ** kwargs ) , how ) ( ) <EOL> def _resample_series ( series , start , end , reindex_closed , rule , <EOL> resample_kwargs , how , fill_value ) : <EOL> out = _resample_apply ( series , rule , how , resample_kwargs ) <EOL> return out . reindex ( pd . date_range ( start , end , freq = rule , <EOL> closed = reindex_closed ) , <EOL> fill_value = fill_value ) <EOL> def _resample_bin_and_out_divs ( divisions , rule , closed = '<STR_LIT:left>' , label = '<STR_LIT:left>' ) : <EOL> rule = pd . datetools . to_offset ( rule ) <EOL> g = pd . TimeGrouper ( rule , how = '<STR_LIT:count>' , closed = closed , label = label ) <EOL> divs = pd . Series ( range ( len ( divisions ) ) , index = divisions ) <EOL> temp = divs . resample ( rule , how = '<STR_LIT:count>' , closed = closed , label = '<STR_LIT:left>' ) <EOL> tempdivs = temp . loc [ temp > <NUM_LIT:0> ] . index <EOL> res = pd . offsets . Nano ( ) if hasattr ( rule , '<STR_LIT>' ) else pd . offsets . Day ( ) <EOL> if g . closed == '<STR_LIT:right>' : <EOL> newdivs = tempdivs + res <EOL> else : <EOL> newdivs = tempdivs <EOL> if g . label == '<STR_LIT:right>' : <EOL> outdivs = tempdivs + rule <EOL> else : <EOL> outdivs = tempdivs <EOL> newdivs = newdivs . tolist ( ) <EOL> outdivs = outdivs . tolist ( ) <EOL> if newdivs [ <NUM_LIT:0> ] < divisions [ <NUM_LIT:0> ] : <EOL> newdivs [ <NUM_LIT:0> ] = divisions [ <NUM_LIT:0> ] <EOL> if newdivs [ - <NUM_LIT:1> ] < divisions [ - <NUM_LIT:1> ] : <EOL> if len ( newdivs ) < len ( divs ) : <EOL> setter = lambda a , val : a . append ( val ) <EOL> else : <EOL> setter = lambda a , val : a . __setitem__ ( - <NUM_LIT:1> , val ) <EOL> setter ( newdivs , divisions [ - <NUM_LIT:1> ] ) <EOL> if outdivs [ - <NUM_LIT:1> ] > divisions [ - <NUM_LIT:1> ] : <EOL> setter ( outdivs , outdivs [ - <NUM_LIT:1> ] ) <EOL> elif outdivs [ - <NUM_LIT:1> ] < divisions [ - <NUM_LIT:1> ] : <EOL> setter ( outdivs , temp . index [ - <NUM_LIT:1> ] ) <EOL> return tuple ( map ( pd . Timestamp , newdivs ) ) , tuple ( map ( pd . Timestamp , outdivs ) ) <EOL> class Resampler ( object ) : <EOL> def __init__ ( self , obj , rule , ** kwargs ) : <EOL> self . obj = obj <EOL> rule = pd . datetools . to_offset ( rule ) <EOL> day_nanos = pd . datetools . Day ( ) . nanos <EOL> if getnanos ( rule ) and day_nanos % rule . nanos : <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % rule ) <EOL> self . _rule = rule <EOL> self . _kwargs = kwargs <EOL> def _agg ( self , how , columns = None , fill_value = np . nan ) : <EOL> rule = self . _rule <EOL> kwargs = self . _kwargs <EOL> name = tokenize ( self . obj , rule , kwargs , how ) <EOL> newdivs , outdivs = _resample_bin_and_out_divs ( self . obj . divisions , rule , <EOL> ** kwargs ) <EOL> partitioned = self . obj . repartition ( newdivs , force = True ) <EOL> keys = partitioned . _keys ( ) <EOL> dsk = partitioned . dask <EOL> args = zip ( keys , outdivs , outdivs [ <NUM_LIT:1> : ] , [ '<STR_LIT:left>' ] * ( len ( keys ) - <NUM_LIT:1> ) + [ None ] ) <EOL> for i , ( k , s , e , c ) in enumerate ( args ) : <EOL> dsk [ ( name , i ) ] = ( _resample_series , k , s , e , c , <EOL> rule , kwargs , how , fill_value ) <EOL> if columns : <EOL> return DataFrame ( dsk , name , columns , outdivs ) <EOL> return Series ( dsk , name , self . obj . name , outdivs ) <EOL> def count ( self ) : <EOL> return self . _agg ( '<STR_LIT:count>' , fill_value = <NUM_LIT:0> ) <EOL> def first ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def last ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def mean ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def min ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def median ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def max ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def ohlc ( self ) : <EOL> return self . _agg ( '<STR_LIT>' , columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def prod ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def sem ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def std ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def sum ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) <EOL> def var ( self ) : <EOL> return self . _agg ( '<STR_LIT>' ) </s>
