bhf_smarthealth.utils.aggregators#

This module contains custom aggregation functions

Attributes#

Functions#

mode_with_later_tiebreak(→ Any)

Calculates the modal value of a series. Where there is a tie, the modal value that lies latest in the sorted series is returned.

filtered_aggregation(→ object | None)

Perform an aggregation function on a filtered Pandas Series

config_filtered_aggs(→ List[functools.partial])

Configures the filtered_aggregation function(s) as a Partial so that aggregation functions passed to a Pandas groupby object can be passed additional arguments.

Module Contents#

bhf_smarthealth.utils.aggregators.logger#
bhf_smarthealth.utils.aggregators.mode_with_later_tiebreak(series: pandas.Series) → Any[source]#

Calculates the modal value of a series. Where there is a tie, the modal value that lies latest in the sorted series is returned.

Parameters:

series (pd.Series) – Pandas series of data to calculate a single modal value from

Returns:

Modal value that is latest in the ordered series

Return type:

Any

Notes

Can be used to return the mode associated with the latest timestamp in timeseries data.

bhf_smarthealth.utils.aggregators.filtered_aggregation(series: pandas.Series, min_value: float, max_value: float, agg_func: str) → object | None[source]#

Perform an aggregation function on a filtered Pandas Series

Parameters:
  • series (pd.Series) – Series of data to be filtered and aggregated

  • min_value (float) – The allowed minimum value (inclusive)

  • max_value (float) – The maximum allowed value (inclusive)

  • agg_func (str) – The aggregation function to be applied to the filtered Series

Returns:

The aggregated value or None if the Series, or filtered Series, is empty

Return type:

float | None

Raises:

ValueError – Raised if an unsupported aggregation function is specified

bhf_smarthealth.utils.aggregators.config_filtered_aggs(min_value: float, max_value: float, agg_funcs: List[str]) → List[functools.partial][source]#

Configures the filtered_aggregation function(s) as a Partial so that aggregation functions passed to a Pandas groupby object can be passed additional arguments.

The function name is manually set to provide an informative column name in the resulting DataFrame.

See https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#groupby-aggregate-named for more information

Parameters:
  • min_value (float) – The minimum allowed value (inclusive)

  • max_value (float) – The maximum allowed value (inclusive)

  • agg_funcs (str) – The aggregation function(s) to be applied to the filtered Series

Returns:

A list of partial objects, see https://docs.python.org/3/library/functools.html#partial-objects

Return type:

List[functools.partial]

Notes

When passing config_filtered_aggs to pandas.agg(), if you want to combine custom filtered aggregations with standard pandas aggregation strings you can use list expansion, i.e. {“column_to_be_aggregated”:
[“list”, “of”, “standard”, “aggregations”]

config_filtered_aggs(min_value, max_value, [“list”, “of”, “filtered”, “aggregations])

}