def max_unit_id_by_plant(gens_df):
    """Identify the largest unit ID associated with each plant so we don't overlap.

    The PUDL Unit IDs are sequentially assigned integers. To assign a new ID, we
    need to know the largest existing Unit ID within a plant. This function
    calculates that largest existing ID, or uses zero, if no Unit IDs are set
    within the plant.

    Note that this calculation depends on having all of the pre-existing
    generators and units still available in the dataframe!

    Args:
        gens_df (pandas.DataFrame): A generators_eia860 dataframe containing at
            least the columns plant_id_eia and unit_id_pudl.

    Returns:
        pandas.DataFrame: Having two columns: plant_id_eia and max_unit_id_pudl
        in which each row should be unique.

    """
    return (
        gens_df[["plant_id_eia", "unit_id_pudl"]]
        .drop_duplicates()
        .groupby("plant_id_eia")
        .agg({"unit_id_pudl": max})
        .fillna(0)
        .rename(columns={"unit_id_pudl": "max_unit_id_pudl"})
        .reset_index()
    )