Skip to content

Commit 455f28d

Browse files
authored
WorlddevelopmentIndicator - added logic to skip file that doesn't exist (#1920)
* added logic to skip file that doesn't exist * added logic to skip file that doesn't exist * modified code
1 parent 3371e36 commit 455f28d

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

scripts/world_bank/wdi/worldbank.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,11 @@ def read_worldbank(iso3166alpha3, mode):
255255
if file.startswith("API"):
256256
file_to_open = file
257257
break
258-
assert file_to_open is not None, \
259-
"Failed to find data for" + iso3166alpha3
260-
258+
if file_to_open is None:
259+
logging.warning(
260+
'Failed to find data for %s in the downloaded ZIP. Skipping.',
261+
iso3166alpha3)
262+
return None
261263
df = None
262264
# Captures any text contained in double quotatations.
263265
line_match = re.compile(r"\"([^\"]*)\"")
@@ -415,15 +417,21 @@ def download_indicator_data(worldbank_countries, indicator_codes, mode):
415417
for index, country_code in enumerate(worldbank_countries['ISO3166Alpha3']):
416418
country_df = read_worldbank(country_code, mode)
417419

420+
if country_df is None:
421+
continue
422+
418423
# Remove unneccessary indicators.
419424
country_df = country_df[country_df['IndicatorCode'].isin(
420425
indicators_to_keep)]
421-
422426
# Map country codes to ISO.
423427
country_df['ISO3166Alpha3'] = country_code
424-
425428
# Add new row to main datframe.
426429
country_df_list.append(country_df)
430+
# 3. Handle the empty list case OUTSIDE the loop
431+
if not country_df_list:
432+
logging.error("No data was downloaded for any country.")
433+
# Return empty DF with expected columns to satisfy the rest of the pipeline
434+
return pd.DataFrame(columns=['StatisticalVariable', 'Year', 'Value'])
427435

428436
worldbank_dataframe = pd.concat(country_df_list)
429437
# Map indicator codes to unique Statistical Variable.

0 commit comments

Comments
 (0)