#NEWLINE# def _add_margins( table , data , values , rows , cols , aggfunc , margins_name = 'All' ) : #NEWLINE# #INDENT# if ( not isinstance ( margins_name , compat . string_types ) ) : #NEWLINE# #INDENT# raise ValueError ( 'margins_name argument must be a string' ) #NEWLINE# #UNINDENT# exception_msg = 'Conflicting name "{0}" in margins' . format ( margins_name ) #NEWLINE# for level in table . index . names : #NEWLINE# #INDENT# if ( margins_name in table . index . get_level_values ( level ) ) : #NEWLINE# #INDENT# raise ValueError ( exception_msg ) #NEWLINE# #UNINDENT# #UNINDENT# grand_margin = _compute_grand_margin ( data , values , aggfunc , margins_name ) #NEWLINE# if hasattr ( table , 'columns' ) : #NEWLINE# #INDENT# for level in table . columns . names [ 1 : ] : #NEWLINE# #INDENT# if ( margins_name in table . columns . get_level_values ( level ) ) : #NEWLINE# #INDENT# raise ValueError ( exception_msg ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( len ( rows ) > 1 ) : #NEWLINE# #INDENT# key = ( ( margins_name , ) + ( ( '' , ) * ( len ( rows ) - 1 ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# key = margins_name #NEWLINE# #UNINDENT# if ( ( not values ) and isinstance ( table , Series ) ) : #NEWLINE# #INDENT# return table . append ( Series ( { #NEWLINE# #INDENT# key : grand_margin [ margins_name ] , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# if values : #NEWLINE# #INDENT# marginal_result_set = _generate_marginal_results ( table , data , values , rows , cols , aggfunc , grand_margin , margins_name ) #NEWLINE# if ( not isinstance ( marginal_result_set , tuple ) ) : #NEWLINE# #INDENT# return marginal_result_set #NEWLINE# #UNINDENT# ( result , margin_keys , row_margin ) = marginal_result_set #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# marginal_result_set = _generate_marginal_results_without_values ( table , data , rows , grand_margin , aggfunc , margins_name ) #NEWLINE# if ( not isinstance ( marginal_result_set , tuple ) ) : #NEWLINE# #INDENT# return marginal_result_set #NEWLINE# #UNINDENT# ( result , margin_keys , row_margin ) = marginal_result_set #NEWLINE# #UNINDENT# row_margin = row_margin . reindex ( result . columns ) #NEWLINE# for k in margin_keys : #NEWLINE# #INDENT# if isinstance ( k , compat . string_types ) : #NEWLINE# #INDENT# row_margin [ k ] = grand_margin [ k ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# row_margin [ k ] = grand_margin [ k [ 0 ] ] #NEWLINE# #UNINDENT# #UNINDENT# margin_dummy = DataFrame ( row_margin , columns = [ key ] ) . T #NEWLINE# row_names = result . index . names #NEWLINE# try : #NEWLINE# #INDENT# result = result . append ( margin_dummy ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# result . index = result . index . _to_safe_for_reshape ( ) #NEWLINE# result = result . append ( margin_dummy ) #NEWLINE# #UNINDENT# result . index . names = row_names #NEWLINE# return result