src/ei_mcoe/ei_mcoe.py
def add_cems_to_eia(part1_df, bga_df, cems_df, raw_eia_df, level):
    """Merge EIA plant or unit level data with CEMS emissions data.

    Args:
        part1_df (pandas.DataFrame): The output from running either
            built_part1_output(pudl_out, 'plant'), or build_part1_output(
            pudl_out, 'unit').
        bga_df (pandas.DataFrame): The output from running
            pudl_out.bga_eia860()
        cems_df (pandas.DataFrame): The output of running get_cems(). Added as
            a parameter so only have to run get_cems() once because it takes a
            while.
        raw_eia_df (pandas.DataFrame): A DataFrame containing EIA data from the
            pudl_out.mcoe() function that has been run through the prep_raw_
            eia() function.
        level (str): An string indicating the level of desired aggregation.
            Either 'plant-fuel' or 'unit-fuel' are acceptable.

    Returns:
        pandas.DataFrame: A DataFrame containing an merge of the part1 output
            and the CEMS emissions data.
    """
    logger.info(' - Adding cems to EIA data')
    id_cols = ['plant_id_eia', 'unit_id_pudl', 'generator_id',
               'report_year']
    cems_cols = ['so2_mass_lbs', 'nox_mass_lbs', 'co2_mass_tons']
    # Add boiler id to EIA data. Boilder id matches (almost) with CEMS unitid.
    eia_with_boiler_id = (
        pd.merge(
            raw_eia_df
            [id_cols + ['plant_id_pudl', 'fuel_type_code_pudl']]
            .pipe(pudl.helpers.convert_cols_dtypes, 'eia'),
            bga_df
            [id_cols + ['boiler_id']]
            .pipe(pudl.helpers.convert_cols_dtypes, 'eia'),
            on=id_cols,
            how='left')
    )
    eia_cems_merge = (
        pd.merge(eia_with_boiler_id, cems_df,
                 on=['plant_id_eia', 'boiler_id', 'report_year'], how='left')
        .groupby(['plant_id_pudl', 'plant_id_eia', 'unit_id_pudl',
                  'fuel_type_code_pudl', 'report_year'])[cems_cols]
        .sum(min_count=1)
        .reset_index())
    eia_cems_agg = (
        eia_cems_merge.groupby(input_dict[level + '_index_cols'])[cems_cols]
        .sum(min_count=1)
        .assign(so2_mass_tons=lambda x: x.so2_mass_lbs / 2000,
                nox_mass_tons=lambda x: x.nox_mass_lbs / 2000)
        .drop(['so2_mass_lbs', 'nox_mass_lbs'], axis=1)
        .reset_index())
    return eia_cems_agg
Merge EIA plant or unit level data with CEMS emissions data.

    Args:
        part1_df (pandas.DataFrame): The output from running either
            built_part1_output(pudl_out, 'plant'), or build_part1_output(
            pudl_out, 'unit').
        bga_df (pandas.DataFrame): The output from running
            pudl_out.bga_eia860()
        cems_df (pandas.DataFrame): The output of running get_cems(). Added as
            a parameter so only have to run get_cems() once because it takes a
            while.
        raw_eia_df (pandas.DataFrame): A DataFrame containing EIA data from the
            pudl_out.mcoe() function that has been run through the prep_raw_
            eia() function.
        level (str): An string indicating the level of desired aggregation.
            Either 'plant-fuel' or 'unit-fuel' are acceptable.

    Returns:
        pandas.DataFrame: A DataFrame containing an merge of the part1 output
            and the CEMS emissions data.
    
Merge EIA plant or unit level data with CEMS emissions data.
Merge EIA plant or unit level data with CEMS emissions data .
def add_cems_to_eia(part1_df, bga_df, cems_df, raw_eia_df, level):
    logger.info(' - Adding cems to EIA data')
    id_cols = ['plant_id_eia', 'unit_id_pudl', 'generator_id',
               'report_year']
    cems_cols = ['so2_mass_lbs', 'nox_mass_lbs', 'co2_mass_tons']
    eia_with_boiler_id = (
        pd.merge(
            raw_eia_df
            [id_cols + ['plant_id_pudl', 'fuel_type_code_pudl']]
            .pipe(pudl.helpers.convert_cols_dtypes, 'eia'),
            bga_df
            [id_cols + ['boiler_id']]
            .pipe(pudl.helpers.convert_cols_dtypes, 'eia'),
            on=id_cols,
            how='left')
    )
    eia_cems_merge = (
        pd.merge(eia_with_boiler_id, cems_df,
                 on=['plant_id_eia', 'boiler_id', 'report_year'], how='left')
        .groupby(['plant_id_pudl', 'plant_id_eia', 'unit_id_pudl',
                  'fuel_type_code_pudl', 'report_year'])[cems_cols]
        .sum(min_count=1)
        .reset_index())
    eia_cems_agg = (
        eia_cems_merge.groupby(input_dict[level + '_index_cols'])[cems_cols]
        .sum(min_count=1)
        .assign(so2_mass_tons=lambda x: x.so2_mass_lbs / 2000,
                nox_mass_tons=lambda x: x.nox_mass_lbs / 2000)
        .drop(['so2_mass_lbs', 'nox_mass_lbs'], axis=1)
        .reset_index())
    return eia_cems_agg
def add_cems_to_eia ( part1_df , bga_df , cems_df , raw_eia_df , level ) : logger . info ( ' - Adding cems to EIA data' ) id_cols = [ 'plant_id_eia' , 'unit_id_pudl' , 'generator_id' , 'report_year' ] cems_cols = [ 'so2_mass_lbs' , 'nox_mass_lbs' , 'co2_mass_tons' ] eia_with_boiler_id = ( pd . merge ( raw_eia_df [ id_cols + [ 'plant_id_pudl' , 'fuel_type_code_pudl' ] ] . pipe ( pudl . helpers . convert_cols_dtypes , 'eia' ) , bga_df [ id_cols + [ 'boiler_id' ] ] . pipe ( pudl . helpers . convert_cols_dtypes , 'eia' ) , on = id_cols , how = 'left' ) ) eia_cems_merge = ( pd . merge ( eia_with_boiler_id , cems_df , on = [ 'plant_id_eia' , 'boiler_id' , 'report_year' ] , how = 'left' ) . groupby ( [ 'plant_id_pudl' , 'plant_id_eia' , 'unit_id_pudl' , 'fuel_type_code_pudl' , 'report_year' ] ) [ cems_cols ] . sum ( min_count = 1 ) . reset_index ( ) ) eia_cems_agg = ( eia_cems_merge . groupby ( input_dict [ level + '_index_cols' ] ) [ cems_cols ] . sum ( min_count = 1 ) . assign ( so2_mass_tons = lambda x : x . so2_mass_lbs / 2000 , nox_mass_tons = lambda x : x . nox_mass_lbs / 2000 ) . drop ( [ 'so2_mass_lbs' , 'nox_mass_lbs' ] , axis = 1 ) . reset_index ( ) ) return eia_cems_agg
Merge EIA plant or unit level data with CEMS emissions data.
Merge EIA plant or unit level data with CEMS emissions data .
"""Merge EIA plant or unit level data with CEMS emissions data.

    Args:
        part1_df (pandas.DataFrame): The output from running either
            built_part1_output(pudl_out, 'plant'), or build_part1_output(
            pudl_out, 'unit').
        bga_df (pandas.DataFrame): The output from running
            pudl_out.bga_eia860()
        cems_df (pandas.DataFrame): The output of running get_cems(). Added as
            a parameter so only have to run get_cems() once because it takes a
            while.
        raw_eia_df (pandas.DataFrame): A DataFrame containing EIA data from the
            pudl_out.mcoe() function that has been run through the prep_raw_
            eia() function.
        level (str): An string indicating the level of desired aggregation.
            Either 'plant-fuel' or 'unit-fuel' are acceptable.

    Returns:
        pandas.DataFrame: A DataFrame containing an merge of the part1 output
            and the CEMS emissions data.
    """ # Add boiler id to EIA data. Boilder id matches (almost) with CEMS unitid.
A DataFrame containing an merge of the part1 output
and the CEMS emissions data.
A DataFrame containing an merge of the part1 output and the CEMS emissions data .
pandas.DataFrame
part1_df
The output from running either
built_part1_output(pudl_out, 'plant'), or build_part1_output(
pudl_out, 'unit').
The output from running either built_part1_output ( pudl_out ' plant ' ) or build_part1_output ( pudl_out ' unit ' ) .
bga_df
The output from running
pudl_out.bga_eia860()
The output from running pudl_out . bga_eia860 ()
cems_df
The output of running get_cems(). Added as
a parameter so only have to run get_cems() once because it takes a
while.
The output of running get_cems () . Added as a parameter so only have to run get_cems () once because it takes a while .
raw_eia_df
A DataFrame containing EIA data from the
pudl_out.mcoe() function that has been run through the prep_raw_
eia() function.
A DataFrame containing EIA data from the pudl_out . mcoe () function that has been run through the prep_raw_ eia () function .
level
An string indicating the level of desired aggregation.
Either 'plant-fuel' or 'unit-fuel' are acceptable.
An string indicating the level of desired aggregation . Either ' plant - fuel ' or ' unit - fuel ' are acceptable .