src/ei_mcoe/ei_mcoe.py
def weighted_average(df, wa_col_dict, idx_cols):
    """Generate a weighted average for multiple columns at once.

    When aggregating the data by plant and fuel type, many of the values can
    be summed. Heat rates and generator age, however, are claculated with a
    weighted average. This function exists because there is no python or numpy
    function to calculate weighted average like there is for .sum() or .mean().

    In this case, the heat rate calculation is based the 'weight' or share
    of generator net generation (net_generation_mwh) and the generator age is
    based on that of the generators' capacity (capacity_mw). As seen in the
    global eia_wa_col_dict dictionary.

    Args:
        df (pandas.DataFrame): A DataFrame containing, at minimum, the columns
            specified in the other parameters wa_col_dict and by_cols.
        wa_col_dict (dict): A dictionary containing keys and values that
            represent the column names for the 'data' and 'weight' values.
        idx_cols (list): A list of the columns to group by when calcuating
            the weighted average value.
    Returns:
        pandas.DataFrame: A DataFrame containing weigted average values for
            specified 'data' columns based on specified 'weight' columns.
            Grouped by an indicated set of columns.
    """
    merge_df = df[idx_cols]
    for data_col, weight_col in wa_col_dict.items():
        logger.debug(' - Calculating weighted average for ' + data_col)
        # df.loc[:, '_data_times_weight'] = df.loc[:, data] * df.loc[:, weight]
        # df.loc[:, '_weight_where_notnull'] = (
        #     df.loc[:, weight] * pd.notnull(df[data]))
        # g = df.groupby(idx_cols)
        # result = g[
        #     '_data_times_weight'].sum() / g['_weight_where_notnull'].sum()
        # del df['_data_times_weight'], df['_weight_where_notnull']
        # result = result.to_frame(name=data).reset_index()

        df['_data_times_weight'] = df[data_col] * df[weight_col]
        df['_weight_where_notnull'] = df.loc[df[data_col].notnull(), weight_col]
        g = df.groupby(idx_cols, observed=True)
        result = (
            g['_data_times_weight'].sum(min_count=1)
            / g['_weight_where_notnull'].sum(min_count=1)
        )
        del df['_data_times_weight'], df['_weight_where_notnull']
        result = (
            result.to_frame(name=data_col).reset_index()
            .pipe(pudl.helpers.convert_cols_dtypes, 'eia')
        )

        merge_df = pd.merge(merge_df, result, on=idx_cols, how='outer')
    return merge_df
Generate a weighted average for multiple columns at once.

    When aggregating the data by plant and fuel type, many of the values can
    be summed. Heat rates and generator age, however, are claculated with a
    weighted average. This function exists because there is no python or numpy
    function to calculate weighted average like there is for .sum() or .mean().

    In this case, the heat rate calculation is based the 'weight' or share
    of generator net generation (net_generation_mwh) and the generator age is
    based on that of the generators' capacity (capacity_mw). As seen in the
    global eia_wa_col_dict dictionary.

    Args:
        df (pandas.DataFrame): A DataFrame containing, at minimum, the columns
            specified in the other parameters wa_col_dict and by_cols.
        wa_col_dict (dict): A dictionary containing keys and values that
            represent the column names for the 'data' and 'weight' values.
        idx_cols (list): A list of the columns to group by when calcuating
            the weighted average value.
    Returns:
        pandas.DataFrame: A DataFrame containing weigted average values for
            specified 'data' columns based on specified 'weight' columns.
            Grouped by an indicated set of columns.
    
Generate a weighted average for multiple columns at once.
When aggregating the data by plant and fuel type, many of the values can
be summed. Heat rates and generator age, however, are claculated with a
weighted average. This function exists because there is no python or numpy
function to calculate weighted average like there is for .sum() or .mean().

In this case, the heat rate calculation is based the 'weight' or share
of generator net generation (net_generation_mwh) and the generator age is
based on that of the generators' capacity (capacity_mw). As seen in the
global eia_wa_col_dict dictionary.
Generate a weighted average for multiple columns at once . When aggregating the data by plant and fuel type many of the values can be summed . Heat rates and generator age however are claculated with a weighted average . This function exists because there is no python or numpy function to calculate weighted average like there is for . sum () or . mean () . In this case the heat rate calculation is based the ' weight ' or share of generator net generation ( net_generation_mwh ) and the generator age is based on that of the generators ' capacity ( capacity_mw ) . As seen in the global eia_wa_col_dict dictionary .
def weighted_average(df, wa_col_dict, idx_cols):
    merge_df = df[idx_cols]
    for data_col, weight_col in wa_col_dict.items():
        logger.debug(' - Calculating weighted average for ' + data_col)
        df['_data_times_weight'] = df[data_col] * df[weight_col]
        df['_weight_where_notnull'] = df.loc[df[data_col].notnull(), weight_col]
        g = df.groupby(idx_cols, observed=True)
        result = (
            g['_data_times_weight'].sum(min_count=1)
            / g['_weight_where_notnull'].sum(min_count=1)
        )
        del df['_data_times_weight'], df['_weight_where_notnull']
        result = (
            result.to_frame(name=data_col).reset_index()
            .pipe(pudl.helpers.convert_cols_dtypes, 'eia')
        )
        merge_df = pd.merge(merge_df, result, on=idx_cols, how='outer')
    return merge_df
def weighted_average ( df , wa_col_dict , idx_cols ) : merge_df = df [ idx_cols ] for data_col , weight_col in wa_col_dict . items ( ) : logger . debug ( ' - Calculating weighted average for ' + data_col ) df [ '_data_times_weight' ] = df [ data_col ] * df [ weight_col ] df [ '_weight_where_notnull' ] = df . loc [ df [ data_col ] . notnull ( ) , weight_col ] g = df . groupby ( idx_cols , observed = True ) result = ( g [ '_data_times_weight' ] . sum ( min_count = 1 ) / g [ '_weight_where_notnull' ] . sum ( min_count = 1 ) ) del df [ '_data_times_weight' ] , df [ '_weight_where_notnull' ] result = ( result . to_frame ( name = data_col ) . reset_index ( ) . pipe ( pudl . helpers . convert_cols_dtypes , 'eia' ) ) merge_df = pd . merge ( merge_df , result , on = idx_cols , how = 'outer' ) return merge_df
Generate a weighted average for multiple columns at once.
Generate a weighted average for multiple columns at once .
"""Generate a weighted average for multiple columns at once.

    When aggregating the data by plant and fuel type, many of the values can
    be summed. Heat rates and generator age, however, are claculated with a
    weighted average. This function exists because there is no python or numpy
    function to calculate weighted average like there is for .sum() or .mean().

    In this case, the heat rate calculation is based the 'weight' or share
    of generator net generation (net_generation_mwh) and the generator age is
    based on that of the generators' capacity (capacity_mw). As seen in the
    global eia_wa_col_dict dictionary.

    Args:
        df (pandas.DataFrame): A DataFrame containing, at minimum, the columns
            specified in the other parameters wa_col_dict and by_cols.
        wa_col_dict (dict): A dictionary containing keys and values that
            represent the column names for the 'data' and 'weight' values.
        idx_cols (list): A list of the columns to group by when calcuating
            the weighted average value.
    Returns:
        pandas.DataFrame: A DataFrame containing weigted average values for
            specified 'data' columns based on specified 'weight' columns.
            Grouped by an indicated set of columns.
    """ # df.loc[:, '_data_times_weight'] = df.loc[:, data] * df.loc[:, weight] # df.loc[:, '_weight_where_notnull'] = ( #     df.loc[:, weight] * pd.notnull(df[data])) # g = df.groupby(idx_cols) # result = g[ #     '_data_times_weight'].sum() / g['_weight_where_notnull'].sum() # del df['_data_times_weight'], df['_weight_where_notnull'] # result = result.to_frame(name=data).reset_index()
A DataFrame containing weigted average values for
specified 'data' columns based on specified 'weight' columns.
Grouped by an indicated set of columns.
A DataFrame containing weigted average values for specified ' data ' columns based on specified ' weight ' columns . Grouped by an indicated set of columns .
pandas.DataFrame
df
A DataFrame containing, at minimum, the columns
specified in the other parameters wa_col_dict and by_cols.
A DataFrame containing at minimum the columns specified in the other parameters wa_col_dict and by_cols .
wa_col_dict
A dictionary containing keys and values that
represent the column names for the 'data' and 'weight' values.
A dictionary containing keys and values that represent the column names for the ' data ' and ' weight ' values .
idx_cols
A list of the columns to group by when calcuating
the weighted average value.
A list of the columns to group by when calcuating the weighted average value .