@@ -870,6 +870,9 @@ def _get_livelihoods_protection_threshold_as_percentage_kcals(self):
870870 )
871871
872872 df = pd .DataFrame .from_records (livelihood_protection_qs )
873+ if not df .empty :
874+ for col in ["expenditure" , "percentage_allocation_to_basket" , "household_size" , "percentage_kcals" ]:
875+ df [col ] = pd .to_numeric (df [col ], errors = "coerce" )
873876 if df .empty :
874877 livelihoods_protection_expenditure = 0
875878 livelihoods_protection_percentage_kcals = 0
@@ -922,8 +925,10 @@ def _get_livelihoods_protection_threshold_as_percentage_kcals(self):
922925 # the current Wealth Group
923926 for column in poor_df .columns :
924927 if column != "livelihood_strategy_id" :
925- merged [f"effective_{ column } " ] = merged [f"{ column } _wealth_group" ].combine_first (
926- merged [f"{ column } _poor" ]
928+ wealth_group_values = merged [f"{ column } _wealth_group" ]
929+ poor_values = merged [f"{ column } _poor" ]
930+ merged [f"effective_{ column } " ] = wealth_group_values .where (
931+ wealth_group_values .notna (), poor_values
927932 )
928933
929934 # Calculate the expenditure for the Basket by applying the
@@ -932,10 +937,14 @@ def _get_livelihoods_protection_threshold_as_percentage_kcals(self):
932937 # was for the Poor Wealth Group the expenditure reflects the
933938 # different household sizes.
934939 merged ["basket_expenditure" ] = (
935- merged ["effective_expenditure" ]
936- * merged ["effective_percentage_allocation_to_basket" ]
937- / merged ["effective_household_size" ]
938- * self .average_household_size
940+ (
941+ merged ["effective_expenditure" ]
942+ * merged ["effective_percentage_allocation_to_basket" ]
943+ / merged ["effective_household_size" ]
944+ * self .average_household_size
945+ )
946+ if self .average_household_size
947+ else None
939948 )
940949
941950 # For the percentage_kcals we can apply the effective_percentage_allocation_to_basket
0 commit comments