|
15 | 15 | import os |
16 | 16 | import sys |
17 | 17 | import numpy as np |
| 18 | +import pandas as pd |
18 | 19 | from absl import logging, flags, app |
19 | 20 | # Import the required function after installing the package |
20 | 21 | from bblocks import WorldBankData, DebtIDS |
|
92 | 93 |
|
93 | 94 | def main(_): |
94 | 95 | # Itterating each list to download the respective data. |
95 | | - for idx, indicator in enumerate(indicator_list): |
| 96 | + for idx, indicator_list_to_process in enumerate(indicator_list): |
96 | 97 | # Creating an IDS object |
97 | 98 | debt_id = DebtIDS() |
98 | | - # Load the indicators. |
99 | | - logging.info("Loaded data for%s", indicator) |
100 | | - debt_id.load_data(indicators=indicator, |
101 | | - start_year=FLAGS.start_year, |
102 | | - end_year=FLAGS.end_year) |
103 | | - # Get the data as a DataFrame |
104 | | - df = debt_id.get_data() |
| 99 | + |
| 100 | + all_data_frames = [] |
| 101 | + |
| 102 | + # Process indicators one by one to avoid crashing the whole batch |
| 103 | + for ind in indicator_list_to_process: |
| 104 | + try: |
| 105 | + logging.info("Loading data for %s", ind) |
| 106 | + debt_id.load_data(indicators=[ind], |
| 107 | + start_year=FLAGS.start_year, |
| 108 | + end_year=FLAGS.end_year) |
| 109 | + # Get the data for this specific indicator |
| 110 | + df_temp = debt_id.get_data() |
| 111 | + if df_temp is not None and not df_temp.empty: |
| 112 | + all_data_frames.append(df_temp) |
| 113 | + |
| 114 | + # Reset debt_id for next indicator if needed or use a new one |
| 115 | + debt_id = DebtIDS() |
| 116 | + except Exception as e: |
| 117 | + logging.error("Failed to load data for %s: %s", ind, e) |
| 118 | + continue |
| 119 | + |
| 120 | + if not all_data_frames: |
| 121 | + logging.warning("No data loaded for category: %s", indicator_listname[idx]) |
| 122 | + continue |
| 123 | + |
| 124 | + # Combine all successful indicators for this category |
| 125 | + df = pd.concat(all_data_frames, ignore_index=True) |
| 126 | + |
105 | 127 | try: |
106 | 128 | # Extracting only the year part for data. |
107 | 129 | df["YEARMOD"]=df["year"].dt.year |
|
0 commit comments