<s> """<STR_LIT>""" <EOL> import itertools <EOL> import operator <EOL> import sys <EOL> import types <EOL> import warnings <EOL> warnings . simplefilter ( "<STR_LIT>" ) <EOL> import six <EOL> from six . moves import range <EOL> import numpy as np <EOL> import pandas <EOL> from pandas import DataFrame <EOL> __version__ = "<STR_LIT>" <EOL> class Manager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , attr ) : <EOL> return Later ( attr ) <EOL> def __getitem__ ( self , key ) : <EOL> return Later ( key ) <EOL> X = Manager ( ) <EOL> reversible_operators = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ] <EOL> normal_operators = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ] <EOL> def create_reversible_func ( func_name ) : <EOL> def reversible_func ( self , arg ) : <EOL> self . _UpdateStrAttr ( func_name ) <EOL> self . _UpdateStrCallArgs ( [ arg ] , { } ) <EOL> def use_operator ( df ) : <EOL> if isinstance ( arg , Later ) : <EOL> altered_arg = arg . applyFcns ( self . origDf ) <EOL> else : <EOL> altered_arg = arg <EOL> return getattr ( operator , func_name ) ( df , altered_arg ) <EOL> self . todo . append ( use_operator ) <EOL> return self <EOL> return reversible_func <EOL> def instrument_operator_hooks ( cls ) : <EOL> def add_hook ( name ) : <EOL> def op_hook ( self , * args , ** kwargs ) : <EOL> self . _UpdateStrAttr ( name ) <EOL> self . _UpdateStrCallArgs ( args , kwargs ) <EOL> if len ( args ) > <NUM_LIT:0> and type ( args [ <NUM_LIT:0> ] ) == Later : <EOL> self . todo . append ( lambda df : getattr ( df , name ) ( args [ <NUM_LIT:0> ] . applyFcns ( self . origDf ) ) ) <EOL> else : <EOL> self . todo . append ( lambda df : getattr ( df , name ) ( * args , ** kwargs ) ) <EOL> return self <EOL> try : <EOL> setattr ( cls , name , op_hook ) <EOL> except ( AttributeError , TypeError ) : <EOL> pass <EOL> for hook_name in normal_operators : <EOL> add_hook ( hook_name ) <EOL> for func_name , rfunc_name in reversible_operators : <EOL> setattr ( cls , func_name , create_reversible_func ( func_name ) ) <EOL> return cls <EOL> def _addQuotes ( item ) : <EOL> return '<STR_LIT:">' + item + '<STR_LIT:">' if isinstance ( item , str ) else item <EOL> @ instrument_operator_hooks <EOL> class Later ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> if name == "<STR_LIT:_>" : <EOL> self . todo = [ lambda df : df ] <EOL> else : <EOL> self . todo = [ lambda df : df [ self . name ] ] <EOL> self . _str = '<STR_LIT>' . format ( name ) <EOL> def applyFcns ( self , df ) : <EOL> self . origDf = df <EOL> stmt = df <EOL> for func in self . todo : <EOL> stmt = func ( stmt ) <EOL> return stmt <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . _str ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . _str ) <EOL> def __getattr__ ( self , attr ) : <EOL> self . todo . append ( lambda df : getattr ( df , attr ) ) <EOL> self . _UpdateStrAttr ( attr ) <EOL> return self <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> self . todo . append ( lambda foo : foo . __call__ ( * args , ** kwargs ) ) <EOL> self . _UpdateStrCallArgs ( args , kwargs ) <EOL> return self <EOL> def __rrshift__ ( self , df ) : <EOL> otherDf = DplyFrame ( df . copy ( deep = True ) ) <EOL> return self . applyFcns ( otherDf ) <EOL> def _UpdateStrAttr ( self , attr ) : <EOL> self . _str += "<STR_LIT>" . format ( attr ) <EOL> def _UpdateStrCallArgs ( self , args , kwargs ) : <EOL> kwargs_strs = sorted ( [ "<STR_LIT>" . format ( k , _addQuotes ( v ) ) <EOL> for k , v in kwargs . items ( ) ] ) <EOL> input_strs = list ( map ( str , args ) ) + kwargs_strs <EOL> input_str = "<STR_LIT:U+002CU+0020>" . join ( input_strs ) <EOL> self . _str += "<STR_LIT>" . format ( input_str ) <EOL> def CreateLaterFunction ( fcn , * args , ** kwargs ) : <EOL> laterFcn = Later ( fcn . __name__ ) <EOL> laterFcn . fcn = fcn <EOL> laterFcn . args = args <EOL> laterFcn . kwargs = kwargs <EOL> def apply_function ( self , df ) : <EOL> self . origDf = df <EOL> args = [ a . applyFcns ( self . origDf ) if type ( a ) == Later else a <EOL> for a in self . args ] <EOL> kwargs = { k : v . applyFcns ( self . origDf ) if type ( v ) == Later else v <EOL> for k , v in six . iteritems ( self . kwargs ) } <EOL> return self . fcn ( * args , ** kwargs ) <EOL> laterFcn . todo = [ lambda df : apply_function ( laterFcn , df ) ] <EOL> laterFcn . _str = '<STR_LIT>' . format ( fcn . __name__ ) <EOL> laterFcn . _UpdateStrCallArgs ( args , kwargs ) <EOL> return laterFcn <EOL> def DelayFunction ( fcn ) : <EOL> def DelayedFcnCall ( * args , ** kwargs ) : <EOL> checkIfLater = lambda x : type ( x ) == Later <EOL> if ( len ( [ a for a in args if isinstance ( a , Later ) ] ) == <NUM_LIT:0> and <EOL> len ( [ v for k , v in kwargs . items ( ) if isinstance ( a , Later ) ] ) == <NUM_LIT:0> ) : <EOL> return fcn ( * args , ** kwargs ) <EOL> else : <EOL> return CreateLaterFunction ( fcn , * args , ** kwargs ) <EOL> return DelayedFcnCall <EOL> class DplyFrame ( DataFrame ) : <EOL> """<STR_LIT>""" <EOL> _metadata = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DplyFrame , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _grouped_on = None <EOL> self . _current_group = None <EOL> self . _grouped_self = None <EOL> if len ( args ) == <NUM_LIT:1> and isinstance ( args [ <NUM_LIT:0> ] , DplyFrame ) : <EOL> self . _copy_attrs ( args [ <NUM_LIT:0> ] ) <EOL> def _copy_attrs ( self , df ) : <EOL> for attr in self . _metadata : <EOL> self . __dict__ [ attr ] = getattr ( df , attr , None ) <EOL> @ property <EOL> def _constructor ( self ) : <EOL> return DplyFrame <EOL> def group_self ( self , names ) : <EOL> self . _grouped_on = names <EOL> self . _grouped_self = self . groupby ( names ) <EOL> def ungroup ( self ) : <EOL> self . _grouped_on = None <EOL> self . _grouped_self = None <EOL> def apply_on_groups ( self , delayedFcn ) : <EOL> outDf = self . _grouped_self . apply ( delayedFcn ) <EOL> for grouped_name in outDf . index . names [ : - <NUM_LIT:1> ] : <EOL> if grouped_name in outDf : <EOL> outDf . reset_index ( level = <NUM_LIT:0> , drop = True , inplace = True ) <EOL> else : <EOL> outDf . reset_index ( level = <NUM_LIT:0> , inplace = True ) <EOL> outDf . group_self ( self . _grouped_on ) <EOL> return outDf <EOL> def __rshift__ ( self , delayedFcn ) : <EOL> if type ( delayedFcn ) == Later : <EOL> return delayedFcn . applyFcns ( self ) <EOL> if delayedFcn == UngroupDF : <EOL> otherDf = DplyFrame ( self . copy ( deep = True ) ) <EOL> return delayedFcn ( otherDf ) <EOL> if self . _grouped_self : <EOL> outDf = self . apply_on_groups ( delayedFcn ) <EOL> return outDf <EOL> else : <EOL> otherDf = DplyFrame ( self . copy ( deep = True ) ) <EOL> return delayedFcn ( otherDf ) <EOL> def ApplyToDataframe ( fcn ) : <EOL> def DplyrFcn ( * args , ** kwargs ) : <EOL> data_arg = None <EOL> if len ( args ) > <NUM_LIT:0> and isinstance ( args [ <NUM_LIT:0> ] , pandas . DataFrame ) : <EOL> data_arg = args [ <NUM_LIT:0> ] <EOL> args = args [ <NUM_LIT:1> : ] <EOL> fcn_to_apply = fcn ( * args , ** kwargs ) <EOL> if data_arg is None : <EOL> return fcn_to_apply <EOL> else : <EOL> return data_arg >> fcn_to_apply <EOL> return DplyrFcn <EOL> @ ApplyToDataframe <EOL> def sift ( * args ) : <EOL> """<STR_LIT>""" <EOL> def f ( df ) : <EOL> final_filter = pandas . Series ( [ True for t in range ( len ( df ) ) ] ) <EOL> final_filter . index = df . index <EOL> for arg in args : <EOL> stmt = arg . applyFcns ( df ) <EOL> final_filter = final_filter & stmt <EOL> if final_filter . dtype != bool : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return df [ final_filter ] <EOL> return f <EOL> def dfilter ( * args , ** kwargs ) : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return sift ( * args , ** kwargs ) <EOL> @ ApplyToDataframe <EOL> def select ( * args ) : <EOL> """<STR_LIT>""" <EOL> names = [ column . name for column in args ] <EOL> return lambda df : df [ [ column . name for column in args ] ] <EOL> @ ApplyToDataframe <EOL> def mutate ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def addColumns ( df ) : <EOL> for key , val in six . iteritems ( kwargs ) : <EOL> if type ( val ) == Later : <EOL> df [ key ] = val . applyFcns ( df ) <EOL> else : <EOL> df [ key ] = val <EOL> return df <EOL> return addColumns <EOL> @ ApplyToDataframe <EOL> def group_by ( * args ) : <EOL> def GroupDF ( df ) : <EOL> df . group_self ( [ arg . name for arg in args ] ) <EOL> return df <EOL> return GroupDF <EOL> @ ApplyToDataframe <EOL> def summarize ( ** kwargs ) : <EOL> def CreateSummarizedDf ( df ) : <EOL> input_dict = { k : val . applyFcns ( df ) for k , val in six . iteritems ( kwargs ) } <EOL> if len ( input_dict ) == <NUM_LIT:0> : <EOL> return DplyFrame ( { } , index = index ) <EOL> if hasattr ( df , "<STR_LIT>" ) and df . _current_group : <EOL> input_dict . update ( df . _current_group ) <EOL> index = [ <NUM_LIT:0> ] <EOL> return DplyFrame ( input_dict , index = index ) <EOL> return CreateSummarizedDf <EOL> def UngroupDF ( df ) : <EOL> df . ungroup ( ) <EOL> return df <EOL> @ ApplyToDataframe <EOL> def ungroup ( ) : <EOL> return UngroupDF <EOL> @ ApplyToDataframe <EOL> def arrange ( * args ) : <EOL> """<STR_LIT>""" <EOL> names = [ column . name for column in args ] <EOL> return lambda df : DplyFrame ( df . sort_values ( names ) ) <EOL> @ ApplyToDataframe <EOL> def head ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return lambda df : df . head ( * args , ** kwargs ) <EOL> @ ApplyToDataframe <EOL> def sample_n ( n ) : <EOL> """<STR_LIT>""" <EOL> return lambda df : DplyFrame ( df . sample ( n ) ) <EOL> @ ApplyToDataframe <EOL> def sample_frac ( frac ) : <EOL> """<STR_LIT>""" <EOL> return lambda df : DplyFrame ( df . sample ( frac = frac ) ) <EOL> @ ApplyToDataframe <EOL> def sample ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return lambda df : df . sample ( * args , ** kwargs ) <EOL> @ ApplyToDataframe <EOL> def nrow ( ) : <EOL> return lambda df : len ( df ) <EOL> @ DelayFunction <EOL> def PairwiseGreater ( series1 , series2 ) : <EOL> index = series1 . index <EOL> newSeries = pandas . Series ( [ max ( s1 , s2 ) for s1 , s2 in zip ( series1 , series2 ) ] ) <EOL> newSeries . index = index <EOL> return newSeries </s>
