bucketplot.py
def weighted_quantiles(df, bucket_var, n_bins=10, bucket_wgt=None, validate=True):
    """
    Assign each row of `df` to a bucket by splitting column `bucket_var`
    into `n_bins` weighted quantiles, weighted by `bucket_wgt`.
    
    bucket_var: Column name of the values to find the quantiles.
        Must not be constant (i.e. just one value for all rows).
    n_bins: Target number of quantiles, but could end up with fewer because
        there are only a finite number of potential cut points.
    bucket_wgt: Weights to use to calculate the weighted quantiles.
        If None (default) or 'const' then equal weights are used for all rows.
        Must be non-negative with at least one postive value.
    validate: boolean. Set to False to omit validation checks on inputs.
    
    Returns: df with the additional `bucket` column 
        The `bucket` column is Categorical data type consisting of Intervals
        that partition the interval from 0 to sum(bucket_wgt).
    """
    if bucket_wgt is None:
        bucket_wgt = 'const'
    if bucket_wgt == 'const':
        df = df.assign(const = 1)
    
    if validate:
        if df[bucket_var].nunique() == 1:
            raise ValueError(
                f"weighted_quantiles: bucket_var column '{bucket_var}' "
                "must not be constant"
            )
        if (df[bucket_wgt] < 0).any() or (df[bucket_wgt] == 0).all():
            raise ValueError(
                f"weighted_quantiles: bucket_wgt column '{bucket_wgt}' "
                "must be non-negative with at least one strictly positive value"
            )
    
    res = df.sort_values(bucket_var).assign(
        **{'cum_rows_' + col: lambda df: (
            df[bucket_wgt].cumsum()
        ) for col in [bucket_wgt]},
        # Ensure that the quantiles cannot split rows with the same value of bucket_var
        **{'cum_' + col: lambda df: (
            df.groupby(bucket_var)['cum_rows_' + col].transform('max')
        ) for col in [bucket_wgt]},
        bucket=lambda df: pd.qcut(df['cum_' + bucket_wgt], q=n_bins, duplicates='drop'),
    )
    return(res)

    Assign each row of `df` to a bucket by splitting column `bucket_var`
    into `n_bins` weighted quantiles, weighted by `bucket_wgt`.
    
    bucket_var: Column name of the values to find the quantiles.
        Must not be constant (i.e. just one value for all rows).
    n_bins: Target number of quantiles, but could end up with fewer because
        there are only a finite number of potential cut points.
    bucket_wgt: Weights to use to calculate the weighted quantiles.
        If None (default) or 'const' then equal weights are used for all rows.
        Must be non-negative with at least one postive value.
    validate: boolean. Set to False to omit validation checks on inputs.
    
    Returns: df with the additional `bucket` column 
        The `bucket` column is Categorical data type consisting of Intervals
        that partition the interval from 0 to sum(bucket_wgt).
    


Column name of the values to find the quantiles.
Must not be constant .
n_bins: Target number of quantiles, but could end up with fewer because
there are only a finite number of potential cut points.
bucket_wgt: Weights to use to calculate the weighted quantiles.
If None (default) or 'const' then equal weights are used for all rows.
Must be non-negative with at least one postive value.
validate: boolean. Set to False to omit validation checks on inputs.

df with the additional `bucket` column
The `bucket` column is Categorical data type consisting of Intervals
that partition the interval from 0 to sum(bucket_wgt).
Column name of the values to find the quantiles . Must not be constant . n_bins : Target number of quantiles but could end up with fewer because there are only a finite number of potential cut points . bucket_wgt : Weights to use to calculate the weighted quantiles . If None ( default ) or ' const ' then equal weights are used for all rows . Must be non - negative with at least one postive value . validate : boolean . Set to False to omit validation checks on inputs . df with the additional ` bucket ` column The ` bucket ` column is Categorical data type consisting of Intervals that partition the interval from 0 to sum ( bucket_wgt ) .
def weighted_quantiles(df, bucket_var, n_bins=10, bucket_wgt=None, validate=True):
    if bucket_wgt is None:
        bucket_wgt = 'const'
    if bucket_wgt == 'const':
        df = df.assign(const = 1)
    if validate:
        if df[bucket_var].nunique() == 1:
            raise ValueError(
                f"weighted_quantiles: bucket_var column '{bucket_var}' "
                "must not be constant"
            )
        if (df[bucket_wgt] < 0).any() or (df[bucket_wgt] == 0).all():
            raise ValueError(
                f"weighted_quantiles: bucket_wgt column '{bucket_wgt}' "
                "must be non-negative with at least one strictly positive value"
            )
    res = df.sort_values(bucket_var).assign(
        **{'cum_rows_' + col: lambda df: (
            df[bucket_wgt].cumsum()
        ) for col in [bucket_wgt]},
        **{'cum_' + col: lambda df: (
            df.groupby(bucket_var)['cum_rows_' + col].transform('max')
        ) for col in [bucket_wgt]},
        bucket=lambda df: pd.qcut(df['cum_' + bucket_wgt], q=n_bins, duplicates='drop'),
    )
    return(res)
def weighted_quantiles ( df , bucket_var , n_bins = 10 , bucket_wgt = None , validate = True ) : if bucket_wgt is None : bucket_wgt = 'const' if bucket_wgt == 'const' : df = df . assign ( const = 1 ) if validate : if df [ bucket_var ] . nunique ( ) == 1 : raise ValueError ( f"weighted_quantiles: bucket_var column '{bucket_var}' " "must not be constant" ) if ( df [ bucket_wgt ] < 0 ) . any ( ) or ( df [ bucket_wgt ] == 0 ) . all ( ) : raise ValueError ( f"weighted_quantiles: bucket_wgt column '{bucket_wgt}' " "must be non-negative with at least one strictly positive value" ) res = df . sort_values ( bucket_var ) . assign ( ** { 'cum_rows_' + col : lambda df : ( df [ bucket_wgt ] . cumsum ( ) ) for col in [ bucket_wgt ] } , ** { 'cum_' + col : lambda df : ( df . groupby ( bucket_var ) [ 'cum_rows_' + col ] . transform ( 'max' ) ) for col in [ bucket_wgt ] } , bucket = lambda df : pd . qcut ( df [ 'cum_' + bucket_wgt ] , q = n_bins , duplicates = 'drop' ) , ) return ( res )
Assign each row of `df` to a bucket by splitting column `bucket_var`
into `n_bins` weighted quantiles, weighted by `bucket_wgt`.
Assign each row of ` df ` to a bucket by splitting column ` bucket_var ` into ` n_bins ` weighted quantiles weighted by ` bucket_wgt ` .
"""
    Assign each row of `df` to a bucket by splitting column `bucket_var`
    into `n_bins` weighted quantiles, weighted by `bucket_wgt`.
    
    bucket_var: Column name of the values to find the quantiles.
        Must not be constant (i.e. just one value for all rows).
    n_bins: Target number of quantiles, but could end up with fewer because
        there are only a finite number of potential cut points.
    bucket_wgt: Weights to use to calculate the weighted quantiles.
        If None (default) or 'const' then equal weights are used for all rows.
        Must be non-negative with at least one postive value.
    validate: boolean. Set to False to omit validation checks on inputs.
    
    Returns: df with the additional `bucket` column 
        The `bucket` column is Categorical data type consisting of Intervals
        that partition the interval from 0 to sum(bucket_wgt).
    """ # Ensure that the quantiles cannot split rows with the same value of bucket_var
df
bucket_var
n_bins
bucket_wgt
validate