Skip to content

Commit 160001f

Browse files
committed
Update reading WB data
* Fall back to parsing dates if conversion to ints fails. * Throw a ValueError if no data is available.
1 parent e29a049 commit 160001f

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

climada/util/finance.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ def download_world_bank_indicator(
191191
The country code in ISO alpha 3
192192
indicator : str
193193
The ID of the indicator in the World Bank API
194-
parse_dates : bool
194+
parse_dates : bool, optional
195195
Whether the dates of the indicator data should be parsed as datetime objects.
196-
If ``False`` (default), they will be parsed as ``int`` (this only works for
197-
yearly data).
196+
If ``False`` (default), this will first try to parse them as ``int`` (this only
197+
works for yearly data), and then parse as datetime objects if that fails.
198198
199199
Returns
200200
-------
@@ -224,19 +224,28 @@ def download_world_bank_indicator(
224224
except KeyError:
225225
pass
226226

227-
# Update the data
227+
# Check if there is no data available
228228
pages = json_data[0]["pages"]
229+
if pages == 0:
230+
raise ValueError(
231+
f"No data available for country {country_code}, indicator {indicator}"
232+
)
233+
234+
# Update the data
229235
page = page + 1
230236
raw_data.extend(json_data[1])
231237

232238
# Create dataframe
233239
data = pd.DataFrame.from_records(raw_data)
234240

235-
# Parse dates
241+
# Maybe parse dates
236242
if parse_dates:
237243
data["date"] = pd.DatetimeIndex(data["date"])
238244
else:
239-
data["date"] = data["date"].astype("int")
245+
try:
246+
data["date"] = data["date"].astype("int")
247+
except TypeError:
248+
data["date"] = pd.DatetimeIndex(data["date"])
240249

241250
# Only return indicator data (with a proper name)
242251
return data.set_index("date")["value"].rename(data["indicator"].iloc[0]["value"])

climada/util/test/test_finance.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ def test_download_wb_data(self):
160160
"Did you use the correct country code",
161161
):
162162
download_world_bank_indicator("ESP", "BogusIndicator")
163+
with self.assertRaisesRegex(
164+
ValueError,
165+
"No data available for country AIA, indicator NY.GDP.MKTP.CD",
166+
):
167+
download_world_bank_indicator("AIA", "NY.GDP.MKTP.CD")
163168

164169

165170
class TestWealth2GDP(unittest.TestCase):

0 commit comments

Comments
 (0)