(total, data, accounting_column, prob_column=None)
return shuffle.loc[sample_idx].copy(), matched
Samples rows with accounting without replacement.

    Parameters
    ----------
    total : int
        The control total the sampled rows will attempt to match.
    data: pandas.DataFrame
        Table to sample from.
    accounting_column: string
        Name of column with accounting totals/quantities to apply towards the control.
    prob_column: string, optional, default None
        Name of the column in the data to provide probabilities or weights.

    Returns
    -------
    sample_rows : pandas.DataFrame
        Table containing the sample.
    matched: bool
        Indicates if the total was matched exactly.
Samples rows with accounting without replacement.
Samples rows with accounting without replacement .
def accounting_sample_no_replace(total, data, accounting_column, prob_column=None):
    """
    Samples rows with accounting without replacement.

    Parameters
    ----------
    total : int
        The control total the sampled rows will attempt to match.
    data: pandas.DataFrame
        Table to sample from.
    accounting_column: string
        Name of column with accounting totals/quantities to apply towards the control.
    prob_column: string, optional, default None
        Name of the column in the data to provide probabilities or weights.

    Returns
    -------
    sample_rows : pandas.DataFrame
        Table containing the sample.
    matched: bool
        Indicates if the total was matched exactly.

    """
    # make sure this is even feasible
    if total > data[accounting_column].sum():
        raise ValueError('Control total exceeds the available samples')

    # check for probabilities
    p = get_probs(data, prob_column)

    # shuffle the rows
    if p is None:
        # random shuffle
        shuff_idx = np.random.permutation(data.index.values)
    else:
        # weighted shuffle
        ran_p = pd.Series(np.power(np.random.rand(len(p)), 1.0 / p), index=data.index)
        ran_p.sort_values(ascending=False)
        shuff_idx = ran_p.index.values

    # get the initial sample
    shuffle = data.loc[shuff_idx]
    csum = np.cumsum(shuffle[accounting_column].values)
    pos = np.searchsorted(csum, total, 'right')
    sample = shuffle.iloc[:pos]

    # refine the sample
    sample_idx = sample.index.values
    sample_total = sample[accounting_column].sum()
    shortage = total - sample_total
    matched = False

    for idx, row in shuffle.iloc[pos:].iterrows():
        if shortage == 0:
            # we've matached
            matched = True
            break

        # add the current element if it doesnt exceed the total
        cnt = row[accounting_column]
        if cnt <= shortage:
            sample_idx = np.append(sample_idx, idx)
            shortage -= cnt

    return shuffle.loc[sample_idx].copy(), matched
def accounting_sample_no_replace ( total , data , accounting_column , prob_column = None ) : # make sure this is even feasible if total > data [ accounting_column ] . sum ( ) : raise ValueError ( 'Control total exceeds the available samples' ) # check for probabilities p = get_probs ( data , prob_column ) # shuffle the rows if p is None : # random shuffle shuff_idx = np . random . permutation ( data . index . values ) else : # weighted shuffle ran_p = pd . Series ( np . power ( np . random . rand ( len ( p ) ) , 1.0 / p ) , index = data . index ) ran_p . sort_values ( ascending = False ) shuff_idx = ran_p . index . values # get the initial sample shuffle = data . loc [ shuff_idx ] csum = np . cumsum ( shuffle [ accounting_column ] . values ) pos = np . searchsorted ( csum , total , 'right' ) sample = shuffle . iloc [ : pos ] # refine the sample sample_idx = sample . index . values sample_total = sample [ accounting_column ] . sum ( ) shortage = total - sample_total matched = False for idx , row in shuffle . iloc [ pos : ] . iterrows ( ) : if shortage == 0 : # we've matached matched = True break # add the current element if it doesnt exceed the total cnt = row [ accounting_column ] if cnt <= shortage : sample_idx = np . append ( sample_idx , idx ) shortage -= cnt return shuffle . loc [ sample_idx ] . copy ( ) , matched