From 41f3df570ee4528481cf93c1b8d1bdfcbee91688 Mon Sep 17 00:00:00 2001 From: satsbaba <106560507+satsbaba@users.noreply.github.com> Date: Fri, 1 Jul 2022 16:48:17 +0200 Subject: [PATCH] Hotfix for weighted median premium (#180) * hotfix for empty array handling --- api/utils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/api/utils.py b/api/utils.py index 816a8159..355dd77b 100644 --- a/api/utils.py +++ b/api/utils.py @@ -185,10 +185,11 @@ def compute_avg_premium(queryset): total_volume = sum(volumes) # weighted_median_premium is the weighted median of the premiums by volume - - weighted_median_premium = weighted_median(values=premiums, - sample_weight=volumes, - quantiles=0.5, - values_sorted=False) - + if len(premiums) > 0 and len(volumes)>0: + weighted_median_premium = weighted_median(values=premiums, + sample_weight=volumes, + quantiles=0.5, + values_sorted=False) + else: + weighted_median_premium = 0.0 return weighted_median_premium, total_volume