bucketplot.py
def all_levels(df, bucket_var, include_levels=None, ret_map=False):
    """
    Assign each row of `df` to a bucket according to the unique 
    values of `bucket_var`.
    
    bucket_var: Column name of the values to split on.
        Missing values will not be assigned to an interval.
    include_levels: Level values to guarantee to include 
        even if they do not appear in the values of bucket_var.
        Missing values are ignored.
    
    Returns: 
        df with the additional `bucket` column
            The `bucket` column is Categorical data type consisting of 
            Intervals that partition a range, plus possible NaN.
        If ret_map is True, also return a Series mapping bucket values
            to bucket intervals.
    """
    # Format inputs
    if include_levels is not None:
        if not isinstance(include_levels, pd.Series):
            include_levels = pd.Series(include_levels)
    
    # Get the mapping from level value to an appropriate interval
    buckets_vals = pd.concat([
        df[bucket_var], include_levels
    ]).drop_duplicates().sort_values(
    ).reset_index(drop=True).dropna().to_frame('val')
    
    # Add a column of intervals (there may be some intervals with no rows)
    if np.issubdtype(df[bucket_var].dtype, np.number):
        # If the values are numeric then take the smallest width
        min_diff = np.min(np.diff(buckets_vals['val']))
        buckets_map = buckets_vals.assign(
            interval=lambda df: pd.cut(df['val'], pd.interval_range(
                start=df['val'].min() - min_diff/2,
                end=df['val'].max() + min_diff/2,
                freq=min_diff
            ))
        )
    else:
        buckets_map = buckets_vals.assign(
            interval=lambda df: pd.interval_range(start=0., periods=df.shape[0], freq=1.)
        )
    
    # Convert to a Series
    buckets_map = buckets_map.reset_index(drop=True)
    
    # Assign buckets and map to intervals
    res = df.assign(
        bucket=lambda df: df[bucket_var].astype(
            pd.CategoricalDtype(buckets_map['val']) 
        ).cat.rename_categories(
            buckets_map.set_index('val')['interval']
        )
    )
    
    if ret_map:
        return(res, buckets_map)
    return(res)

    Assign each row of `df` to a bucket according to the unique 
    values of `bucket_var`.
    
    bucket_var: Column name of the values to split on.
        Missing values will not be assigned to an interval.
    include_levels: Level values to guarantee to include 
        even if they do not appear in the values of bucket_var.
        Missing values are ignored.
    
    Returns: 
        df with the additional `bucket` column
            The `bucket` column is Categorical data type consisting of 
            Intervals that partition a range, plus possible NaN.
        If ret_map is True, also return a Series mapping bucket values
            to bucket intervals.
    
Assign each row of `df` to a bucket according to the unique
values of `bucket_var`.

Column name of the values to split on.
Missing values will not be assigned to an interval.
include_levels: Level values to guarantee to include
even if they do not appear in the values of bucket_var.
Missing values are ignored.
Assign each row of ` df ` to a bucket according to the unique values of ` bucket_var ` . Column name of the values to split on . Missing values will not be assigned to an interval . include_levels : Level values to guarantee to include even if they do not appear in the values of bucket_var . Missing values are ignored .
def all_levels(df, bucket_var, include_levels=None, ret_map=False):
    if include_levels is not None:
        if not isinstance(include_levels, pd.Series):
            include_levels = pd.Series(include_levels)
    buckets_vals = pd.concat([
        df[bucket_var], include_levels
    ]).drop_duplicates().sort_values(
    ).reset_index(drop=True).dropna().to_frame('val')
    if np.issubdtype(df[bucket_var].dtype, np.number):
        min_diff = np.min(np.diff(buckets_vals['val']))
        buckets_map = buckets_vals.assign(
            interval=lambda df: pd.cut(df['val'], pd.interval_range(
                start=df['val'].min() - min_diff/2,
                end=df['val'].max() + min_diff/2,
                freq=min_diff
            ))
        )
    else:
        buckets_map = buckets_vals.assign(
            interval=lambda df: pd.interval_range(start=0., periods=df.shape[0], freq=1.)
        )
    buckets_map = buckets_map.reset_index(drop=True)
    res = df.assign(
        bucket=lambda df: df[bucket_var].astype(
            pd.CategoricalDtype(buckets_map['val']) 
        ).cat.rename_categories(
            buckets_map.set_index('val')['interval']
        )
    )
    if ret_map:
        return(res, buckets_map)
    return(res)
def all_levels ( df , bucket_var , include_levels = None , ret_map = False ) : if include_levels is not None : if not isinstance ( include_levels , pd . Series ) : include_levels = pd . Series ( include_levels ) buckets_vals = pd . concat ( [ df [ bucket_var ] , include_levels ] ) . drop_duplicates ( ) . sort_values ( ) . reset_index ( drop = True ) . dropna ( ) . to_frame ( 'val' ) if np . issubdtype ( df [ bucket_var ] . dtype , np . number ) : min_diff = np . min ( np . diff ( buckets_vals [ 'val' ] ) ) buckets_map = buckets_vals . assign ( interval = lambda df : pd . cut ( df [ 'val' ] , pd . interval_range ( start = df [ 'val' ] . min ( ) - min_diff / 2 , end = df [ 'val' ] . max ( ) + min_diff / 2 , freq = min_diff ) ) ) else : buckets_map = buckets_vals . assign ( interval = lambda df : pd . interval_range ( start = 0. , periods = df . shape [ 0 ] , freq = 1. ) ) buckets_map = buckets_map . reset_index ( drop = True ) res = df . assign ( bucket = lambda df : df [ bucket_var ] . astype ( pd . CategoricalDtype ( buckets_map [ 'val' ] ) ) . cat . rename_categories ( buckets_map . set_index ( 'val' ) [ 'interval' ] ) ) if ret_map : return ( res , buckets_map ) return ( res )
Assign each row of `df` to a bucket according to the unique
values of `bucket_var`.
Assign each row of ` df ` to a bucket according to the unique values of ` bucket_var ` .
"""
    Assign each row of `df` to a bucket according to the unique 
    values of `bucket_var`.
    
    bucket_var: Column name of the values to split on.
        Missing values will not be assigned to an interval.
    include_levels: Level values to guarantee to include 
        even if they do not appear in the values of bucket_var.
        Missing values are ignored.
    
    Returns: 
        df with the additional `bucket` column
            The `bucket` column is Categorical data type consisting of 
            Intervals that partition a range, plus possible NaN.
        If ret_map is True, also return a Series mapping bucket values
            to bucket intervals.
    """ # Format inputs # Get the mapping from level value to an appropriate interval # Add a column of intervals (there may be some intervals with no rows) # If the values are numeric then take the smallest width # Convert to a Series # Assign buckets and map to intervals
df with the additional `bucket` column
The `bucket` column is Categorical data type consisting of
Intervals that partition a range, plus possible NaN.
If ret_map is True, also return a Series mapping bucket values
to bucket intervals.
df with the additional ` bucket ` column The ` bucket ` column is Categorical data type consisting of Intervals that partition a range plus possible NaN . If ret_map is True also return a Series mapping bucket values to bucket intervals .
df
bucket_var
include_levels
ret_map