def index_joins(df, downstream_col="downstream", upstream_col="upstream"):
    """Create an index of joins based on a given segment id.
    Returns a dataframe indexed by id, listing the id of the next
    segment upstream and the id of the next segment downstream.

    Segments that have 0 for both upstream and downstream are 1 segment long.

    WARNING: 0 has special meaning, it denotes that there is no corresponding
    segment.

    Parameters
    ----------
    df : DataFrame
        data frame containing the joins.
    downstream_col : str, optional (default "downstream")
        downstream column name
    upstream_col : str, optional (default "upstream")
        upstream column name

    Returns
    -------
    DataFrame
    """

    df = (
        df.loc[df[downstream_col] != 0]
        .set_index(downstream_col)[[upstream_col]]
        .join(df.set_index(upstream_col)[[downstream_col]])
    )
    df.index.name = "index"

    return df.reset_index().drop_duplicates().set_index("index")