Skip to content

Commit aa8e502

Browse files
authored
Merge pull request #160 from FEWS-NET/HEA-615/ValueError_could-not-convert-string-to-float
Strip and safer conversion of values to float to avoid error see HEA-615
2 parents a70119b + eca6ea8 commit aa8e502

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

pipelines/assets/wealth_characteristic.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,50 @@ def wealth_characteristic_instances(
317317

318318
wealth_group_characteristic_value["reference_type"] = reference_type
319319

320-
# The percentagme of households should be stored as a number between 1 and 100,
320+
# The percentage of households should be stored as a number between 1 and 100,
321321
# but may be stored in the BSS (particularly in the summary column) as a
322322
# decimal fraction between 0 and 1, so correct those values
323-
if (
324-
wealth_group_characteristic_value["wealth_characteristic_id"] == "percentage of households"
325-
and value != ""
326-
and float(value) < 1
327-
):
328-
value = float(value) * 100
323+
try:
324+
if (
325+
wealth_group_characteristic_value["wealth_characteristic_id"]
326+
== "percentage of households"
327+
and str(value).strip()
328+
and float(value) < 1
329+
):
330+
value = float(value) * 100
331+
except Exception as e:
332+
raise ValueError(
333+
"Error in %s converting percentage of households value '%s' to float from 'WB'!%s%s"
334+
% (partition_key, value, column, row)
335+
) from e
329336

330337
wealth_group_characteristic_value["value"] = value
331338

332339
# If this is the summary, then also save the min and max values
333340
if reference_type == WealthGroupCharacteristicValue.CharacteristicReference.SUMMARY:
334341
min_value = df.loc[row, df.columns[-2]]
335-
if min_value != "" and float(min_value) < 1:
336-
min_value = float(min_value) * 100
337342
max_value = df.loc[row, df.columns[-1]]
338-
if max_value != "" and float(max_value) < 1:
339-
max_value = float(max_value) * 100
343+
# Convert min/max percentage of households values from decimal fractions to percentages
344+
if (
345+
wealth_group_characteristic_value["wealth_characteristic_id"]
346+
== "percentage of households"
347+
):
348+
try:
349+
if str(min_value).strip() and float(min_value) < 1:
350+
min_value = float(min_value) * 100
351+
except Exception as e:
352+
raise ValueError(
353+
"Error in %s converting percentage of households value '%s' to float from 'WB'!%s%s"
354+
% (partition_key, min_value, df.columns[-2], row)
355+
) from e
356+
try:
357+
if str(max_value).strip() and float(max_value) < 1:
358+
max_value = float(max_value) * 100
359+
except Exception as e:
360+
raise ValueError(
361+
"Error in %s converting percentage of households value '%s' to float from 'WB'!%s%s"
362+
% (partition_key, max_value, df.columns[-1], row)
363+
) from e
340364
wealth_group_characteristic_value["min_value"] = min_value
341365
wealth_group_characteristic_value["max_value"] = max_value
342366

0 commit comments

Comments
 (0)