#NEWLINE# def _aggregate( self , arg , * args , **kwargs ) : #NEWLINE# #INDENT# '\n        provide an implementation for the aggregators\n\n        Parameters\n        ----------\n        arg : string, dict, function\n        *args : args to pass on to the function\n        **kwargs : kwargs to pass on to the function\n\n        Returns\n        -------\n        tuple of result, how\n\n        Notes\n        -----\n        how can be a string describe the required post-processing, or\n        None if not required\n        ' #NEWLINE# is_aggregator = ( lambda x : isinstance ( x , ( list , tuple , dict ) ) ) #NEWLINE# is_nested_renamer = False #NEWLINE# _level = kwargs . pop ( '_level' , None ) #NEWLINE# if isinstance ( arg , compat . string_types ) : #NEWLINE# #INDENT# return ( getattr ( self , arg ) ( * args , ** kwargs ) , None ) #NEWLINE# #UNINDENT# if isinstance ( arg , dict ) : #NEWLINE# #INDENT# if ( self . axis != 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Can only pass dict with axis=0' ) #NEWLINE# #UNINDENT# obj = self . _selected_obj #NEWLINE# if any ( ( is_aggregator ( x ) for x in compat . itervalues ( arg ) ) ) : #NEWLINE# #INDENT# new_arg = compat . OrderedDict ( ) #NEWLINE# for ( k , v ) in compat . iteritems ( arg ) : #NEWLINE# #INDENT# if ( not isinstance ( v , ( tuple , list , dict ) ) ) : #NEWLINE# #INDENT# new_arg [ k ] = [ v ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_arg [ k ] = v #NEWLINE# #UNINDENT# if isinstance ( v , dict ) : #NEWLINE# #INDENT# is_nested_renamer = True #NEWLINE# if ( k not in obj . columns ) : #NEWLINE# #INDENT# raise SpecificationError ( 'cannot perform renaming for {0} with a nested dictionary' . format ( k ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# arg = new_arg #NEWLINE# #UNINDENT# from pandas.tools.merge import concat #NEWLINE# def _agg_1dim( name , how , subset = None ) : #NEWLINE# #INDENT# '\n                aggregate a 1-dim with how\n                ' #NEWLINE# colg = self . _gotitem ( name , ndim = 1 , subset = subset ) #NEWLINE# if ( colg . ndim != 1 ) : #NEWLINE# #INDENT# raise SpecificationError ( 'nested dictionary is ambiguous in aggregation' ) #NEWLINE# #UNINDENT# return colg . aggregate ( how , _level = ( ( _level or 0 ) + 1 ) ) #NEWLINE# #UNINDENT# def _agg_2dim( name , how ) : #NEWLINE# #INDENT# '\n                aggregate a 2-dim with how\n                ' #NEWLINE# colg = self . _gotitem ( self . _selection , ndim = 2 , subset = obj ) #NEWLINE# return colg . aggregate ( how , _level = None ) #NEWLINE# #UNINDENT# def _agg( arg , func ) : #NEWLINE# #INDENT# '\n                run the aggregations over the arg with func\n                return an OrderedDict\n                ' #NEWLINE# result = compat . OrderedDict ( ) #NEWLINE# for ( fname , agg_how ) in compat . iteritems ( arg ) : #NEWLINE# #INDENT# result [ fname ] = func ( fname , agg_how ) #NEWLINE# #UNINDENT# return result #NEWLINE# #UNINDENT# keys = list ( compat . iterkeys ( arg ) ) #NEWLINE# result = compat . OrderedDict ( ) #NEWLINE# if is_nested_renamer : #NEWLINE# #INDENT# result = list ( _agg ( arg , _agg_1dim ) . values ( ) ) #NEWLINE# if all ( ( isinstance ( r , dict ) for r in result ) ) : #NEWLINE# #INDENT# ( result , results ) = ( compat . OrderedDict ( ) , result ) #NEWLINE# for r in results : #NEWLINE# #INDENT# result . update ( r ) #NEWLINE# #UNINDENT# keys = list ( compat . iterkeys ( result ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . _selection is not None ) : #NEWLINE# #INDENT# keys = None #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . _selection is not None ) : #NEWLINE# #INDENT# sl = set ( self . _selection_list ) #NEWLINE# if ( len ( sl ) == 1 ) : #NEWLINE# #INDENT# result = _agg ( arg , ( lambda fname , agg_how : _agg_1dim ( self . _selection , agg_how ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not len ( ( sl - set ( compat . iterkeys ( arg ) ) ) ) ) : #NEWLINE# #INDENT# result = _agg ( arg , _agg_1dim ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# result = _agg ( arg , _agg_2dim ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# result = _agg ( arg , _agg_1dim ) #NEWLINE# #UNINDENT# except SpecificationError : #NEWLINE# #INDENT# result = _agg ( arg , _agg_2dim ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if isinstance ( result , list ) : #NEWLINE# #INDENT# result = concat ( result , keys = keys , axis = 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( list ( compat . itervalues ( result ) ) [ 0 ] , com . ABCDataFrame ) : #NEWLINE# #INDENT# result = concat ( [ result [ k ] for k in keys ] , keys = keys , axis = 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# from pandas import DataFrame #NEWLINE# result = DataFrame ( result ) #NEWLINE# #UNINDENT# #UNINDENT# return ( result , True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if hasattr ( arg , '__iter__' ) : #NEWLINE# #INDENT# return ( self . _aggregate_multiple_funcs ( arg , _level = _level ) , None ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# result = None #NEWLINE# #UNINDENT# #UNINDENT# cy_func = self . _is_cython_func ( arg ) #NEWLINE# if ( cy_func and ( not args ) and ( not kwargs ) ) : #NEWLINE# #INDENT# return ( getattr ( self , cy_func ) ( ) , None ) #NEWLINE# #UNINDENT# return ( result , True )