flasc.utilities.energy_ratio_utilities.add_bin_weights

flasc.utilities.energy_ratio_utilities.add_bin_weights#

flasc.utilities.energy_ratio_utilities.add_bin_weights(df_, df_freq_pl=None, bin_cols_without_df_name=None, weight_by='min')[source]#

Add weights to DataFrame bins.

Add weights to DataFrame bins based on either frequency counts or the provided frequency table df_freq_pl.

This function assigns weights to DataFrame bins. If 'df_freq_pl' is provided, these weights are used directly. If 'df_freq_pl' is not provided, the function calculates the weights from the input DataFrame 'df_'. Weights can be determined as either the minimum ('min') or the sum ('sum') of counts.

Parameters:
  • df (DataFrame) -- The input Polars DataFrame containing bins and frequency counts.

  • df_freq_pl (DataFrame, optional) -- A Polars DataFrame containing frequency counts for bins. If not provided, the function will calculate these counts from 'df_'.

  • bin_cols_without_df_name (List, optional) -- List of columns used for grouping bins without 'df_name'.

  • weight_by (str, optional) -- Weight calculation method, either 'min' (minimum count) or 'sum' (sum of counts). Defaults to 'min'.

  • df_ (DataFrame)

Returns:

A tuple containing the modified DataFrame 'df_'

with added weights and the DataFrame

Return type:

Tuple[pl.DataFrame, pl.DataFrame]

'df_freq_pl' with the calculated frequency counts.

Raises:
  • RuntimeError -- If none of the ws/wd bins in data appear in df_freq.

  • UserWarning -- If some bins in data are not in df_freq and will receive a weight of 0.

Parameters:
  • df_ (DataFrame)

  • df_freq_pl (DataFrame | None)

  • bin_cols_without_df_name (List | None)

  • weight_by (str)