text_extensions_for_pandas/spanner/consolidate.py
def consolidate(df: pd.DataFrame, on: str, how: str = "left_to_right") -> pd.DataFrame:
    """
    Eliminate overlap among the spans in one column of a
    :class:`pd.DataFrame`.

    :param df: DataFrame containing spans and other attributes
    :param on: Name of column in `df` on which to perform consolidation
    :param how: What policy to use to decide what spans are considered
        to overlap and which of an overlapping pair will remain after
        consolidation. Available policies:
        * ``left_to_right``: Walk through the spans from left to right, keeping \
        the longest non-overlapping match at each position encountered

    :returns: the rows of `df` that remain after applying the specified
     policy to the spans in the column specified by `on`.
    """
    spans = df[on].values
    if not isinstance(spans, SpanArray):
        raise TypeError(f"Column '{on}' of dataframe is of type "
                        f"{df[on].dtype}, which is not a span type.")
    if how != "left_to_right":
        raise ValueError(f"Receieved '{how}' for `how` argument, but "
                         f"the only valid value for that argument is "
                         f"'left_to_right'.")

    tmp = pd.DataFrame({
        "span": spans,
        "begin": spans.begin,
        "end": spans.end,
        "ix": range(len(spans))}
    ).sort_values(["begin", "end"], ascending=[True, False])

    # Slow-but-correct implementation for now
    ix_to_retain = []  # Type: List[int]
    iloc = 0

    while iloc < len(tmp.index):
        # Loop invariants:
        # * iloc == location of a span that doesn't overlap with any
        #           span in ix_to_retain
        # * All locations before iloc have been processed.

        # Since we sorted by end in DESCENDING order, the current span
        # is guaranteed to be the longest span that begins at its begin
        # offset.
        row = tmp.iloc[iloc]
        cur_end = row["end"]
        cur_ix = row["ix"]
        ix_to_retain.append(cur_ix)

        # Skip other spans that begin before this span ends
        while (iloc < len(tmp.index)
               and tmp.iloc[iloc]["begin"] < cur_end):
            iloc += 1

    return df.iloc[ix_to_retain]

    Eliminate overlap among the spans in one column of a
    :class:`pd.DataFrame`.

    :param df: DataFrame containing spans and other attributes
    :param on: Name of column in `df` on which to perform consolidation
    :param how: What policy to use to decide what spans are considered
        to overlap and which of an overlapping pair will remain after
        consolidation. Available policies:
        * ``left_to_right``: Walk through the spans from left to right, keeping \
        the longest non-overlapping match at each position encountered

    :returns: the rows of `df` that remain after applying the specified
     policy to the spans in the column specified by `on`.
    
Eliminate overlap among the spans in one column of a
Eliminate overlap among the spans in one column of a
def consolidate(df: pd.DataFrame, on: str, how: str = "left_to_right") -> pd.DataFrame:
    spans = df[on].values
    if not isinstance(spans, SpanArray):
        raise TypeError(f"Column '{on}' of dataframe is of type "
                        f"{df[on].dtype}, which is not a span type.")
    if how != "left_to_right":
        raise ValueError(f"Receieved '{how}' for `how` argument, but "
                         f"the only valid value for that argument is "
                         f"'left_to_right'.")
    tmp = pd.DataFrame({
        "span": spans,
        "begin": spans.begin,
        "end": spans.end,
        "ix": range(len(spans))}
    ).sort_values(["begin", "end"], ascending=[True, False])
    ix_to_retain = []  
    iloc = 0
    while iloc < len(tmp.index):
        row = tmp.iloc[iloc]
        cur_end = row["end"]
        cur_ix = row["ix"]
        ix_to_retain.append(cur_ix)
        while (iloc < len(tmp.index)
               and tmp.iloc[iloc]["begin"] < cur_end):
            iloc += 1
    return df.iloc[ix_to_retain]
def consolidate ( df : pd . DataFrame , on : str , how : str = "left_to_right" ) -> pd . DataFrame : spans = df [ on ] . values if not isinstance ( spans , SpanArray ) : raise TypeError ( f"Column '{on}' of dataframe is of type " f"{df[on].dtype}, which is not a span type." ) if how != "left_to_right" : raise ValueError ( f"Receieved '{how}' for `how` argument, but " f"the only valid value for that argument is " f"'left_to_right'." ) tmp = pd . DataFrame ( { "span" : spans , "begin" : spans . begin , "end" : spans . end , "ix" : range ( len ( spans ) ) } ) . sort_values ( [ "begin" , "end" ] , ascending = [ True , False ] ) ix_to_retain = [ ] iloc = 0 while iloc < len ( tmp . index ) : row = tmp . iloc [ iloc ] cur_end = row [ "end" ] cur_ix = row [ "ix" ] ix_to_retain . append ( cur_ix ) while ( iloc < len ( tmp . index ) and tmp . iloc [ iloc ] [ "begin" ] < cur_end ) : iloc += 1 return df . iloc [ ix_to_retain ]
Eliminate overlap among the spans in one column of a
Eliminate overlap among the spans in one column of a
"""
    Eliminate overlap among the spans in one column of a
    :class:`pd.DataFrame`.

    :param df: DataFrame containing spans and other attributes
    :param on: Name of column in `df` on which to perform consolidation
    :param how: What policy to use to decide what spans are considered
        to overlap and which of an overlapping pair will remain after
        consolidation. Available policies:
        * ``left_to_right``: Walk through the spans from left to right, keeping \
        the longest non-overlapping match at each position encountered

    :returns: the rows of `df` that remain after applying the specified
     policy to the spans in the column specified by `on`.
    """ # Slow-but-correct implementation for now # Type: List[int] # Loop invariants: # * iloc == location of a span that doesn't overlap with any #           span in ix_to_retain # * All locations before iloc have been processed. # Since we sorted by end in DESCENDING order, the current span # is guaranteed to be the longest span that begins at its begin # offset. # Skip other spans that begin before this span ends
the rows of `df` that remain after applying the specified
policy to the spans in the column specified by `on`.
the rows of ` df ` that remain after applying the specified policy to the spans in the column specified by ` on ` .
df
pd.DataFrame
DataFrame containing spans and other attributes
DataFrame containing spans and other attributes
on
str
Name of column in `df` on which to perform consolidation
Name of column in ` df ` on which to perform consolidation
how
str
None
class
None