#NEWLINE# def make_interactions( people , hh_id_col , p_type_col ) : #NEWLINE# #INDENT# "\n    Make two Pandas DataFrames associating people IDs with two\n    and three person interactions they have within their households.\n\n    Interactions are strings of numbers representing the makeup\n    of the interaction, e.g. '12' or '341'.\n\n    Note that for two-person interactions the interaction string is ordered\n    with the person from the index in the first position of the string\n    and some other person in the second position. In contrast,\n    the interaction strings for three-person interactions are not ordered.\n    The person from the index may be in any position of the string.\n\n    Parameters\n    ----------\n    people : pandas.DataFrame\n        Table of people data. Must contain at least a household ID\n        column and a categorization of person type.\n    hh_id_col : str\n        Name of the column in `people` that has their household ID.\n    p_type_col : str\n        Name of the column in `people` that contains the person type number.\n\n    Returns\n    -------\n    two_interaction : pandas.DataFrame\n        Interactions between two people. Index will be person IDs taken\n        from the index of `people`.\n        The table will have one column called `interaction`.\n    three_interaction : pandas.DataFrame\n        Interactions between three people. Index will be person IDs taken\n        from the index of `people`.\n        The table will have one column called `interaction`.\n\n    " #NEWLINE# two_fmt = '{}{}' . format #NEWLINE# three_fmt = '{}{}{}' . format #NEWLINE# two = [ ] #NEWLINE# three = [ ] #NEWLINE# two_perm_cache = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# three_combo_cache = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( hh_id , df ) in people . groupby ( hh_id_col , sort = False ) : #NEWLINE# #INDENT# hh_size = len ( df ) #NEWLINE# if ( hh_size == 1 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# ptypes = df [ p_type_col ] . values #NEWLINE# hh_idx = df . index . values #NEWLINE# if ( hh_size in two_perm_cache ) : #NEWLINE# #INDENT# two_perms = two_perm_cache [ hh_size ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# two_perms = list ( itertools . permutations ( np . arange ( hh_size ) , 2 ) ) #NEWLINE# two_perm_cache [ hh_size ] = two_perms #NEWLINE# #UNINDENT# two . extend ( ( ( hh_idx [ pA ] , two_fmt ( * ptypes [ [ pA , pB ] ] ) ) for ( pA , pB ) in two_perms ) ) #NEWLINE# if ( hh_size == 2 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( hh_size in three_combo_cache ) : #NEWLINE# #INDENT# three_combos = three_combo_cache [ hh_size ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# three_combos = list ( itertools . combinations ( np . arange ( hh_size ) , 3 ) ) #NEWLINE# three_combo_cache [ hh_size ] = three_combos #NEWLINE# #UNINDENT# three . extend ( ( ( hh_idx [ p ] , three_fmt ( * ptypes . take ( idx ) ) ) for idx in three_combos for p in idx ) ) #NEWLINE# #UNINDENT# if two : #NEWLINE# #INDENT# ( two_idx , two_val ) = zip ( * two ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( two_idx , two_val ) = ( [ ] , [ ] ) #NEWLINE# #UNINDENT# if three : #NEWLINE# #INDENT# ( three_idx , three_val ) = zip ( * three ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( three_idx , three_val ) = ( [ ] , [ ] ) #NEWLINE# #UNINDENT# return ( pd . DataFrame ( { #NEWLINE# #INDENT# 'interaction' : hh_size , #NEWLINE# #UNINDENT# } , index = two_idx ) , pd . DataFrame ( { #NEWLINE# #INDENT# 'interaction' : three_val , #NEWLINE# #UNINDENT# } , index = three_idx ) )