Fix exposure coordinate extraction for CalcDeltaImpact#1304
Conversation
CalcDeltaClimateCalcDeltaImpact
AndMav
left a comment
There was a problem hiding this comment.
Another error arises after this fix. It seems that lat/long are not stored separately in dedicated columns, but in a geometry column in the WorldPop exposure dataset that i am using. Claude provided me an additional fix and now the code runs without any error.
Additional Proposed fix:
if calc_eai_exp:
exp = self.exp_final_input_var.evaluate()
if {"latitude", "longitude"}.issubset(exp.gdf.columns):
coord_df = exp.gdf[["latitude", "longitude"]]
else:
coord_df = pd.DataFrame({
"latitude": exp.gdf.geometry.y,
"longitude": exp.gdf.geometry.x,
})
else:
coord_df = pd.DataFrame([])
Error after the initial fix
KeyError Traceback (most recent call last)
Cell In[59], line 1
----> 1 output_imp = calc_imp.uncertainty(
2 output_imp,
3 rp=[100],
4 calc_eai_exp=True,
5 processes=4, # processes in parallel
6 chunksize=None
7 )
File /cluster/project/climate/amavrakis/climada_dev/climada/engine/unsequa/calc_delta_climate.py:284, in CalcDeltaImpact.uncertainty(self, unc_sample, rp, calc_eai_exp, calc_at_event, relative_delta, processes, chunksize)
282 if calc_eai_exp:
283 exp = self.exp_final_input_var.evaluate()
--> 284 coord_df = exp.gdf[["latitude", "longitude"]]
285 else:
286 coord_df = pd.DataFrame([])
File ~/.venv/climada-6.1.0/lib/python3.11/site-packages/geopandas/geodataframe.py:1896, in GeoDataFrame.getitem(self, key)
1890 def getitem(self, key):
1891 """
1892 If the result is a column containing only 'geometry', return a
1893 GeoSeries. If it's a DataFrame with any columns of GeometryDtype,
1894 return a GeoDataFrame.
1895 """
-> 1896 result = super().getitem(key)
1897 # Custom logic to avoid waiting for pandas GH51895
1898 # result is not geometry dtype for multi-indexes
1899 if (
1900 pd.api.types.is_scalar(key)
1901 and key == ""
(...)
1904 and not is_geometry_type(result)
1905 ):
File /cluster/software/stacks/2024-06/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.2.0/python-3.11.6-ukhwpjnwzzzizek3pgr75zkbhxros5fq/lib/python3.11/site-packages/pandas/core/frame.py:4108, in DataFrame.getitem(self, key)
4106 if is_iterator(key):
4107 key = list(key)
-> 4108 indexer = self.columns._get_indexer_strict(key, "columns")[1]
4110 # take() does not accept boolean indexers
4111 if getattr(indexer, "dtype", None) == bool:
File /cluster/software/stacks/2024-06/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.2.0/python-3.11.6-ukhwpjnwzzzizek3pgr75zkbhxros5fq/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name)
6197 else:
6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr)
-> 6200 self._raise_if_missing(keyarr, indexer, axis_name)
6202 keyarr = self.take(indexer)
6203 if isinstance(key, Index):
6204 # GH 42790 - Preserve name from an Index
File /cluster/software/stacks/2024-06/spack/opt/spack/linux-ubuntu22.04-x86_64_v3/gcc-12.2.0/python-3.11.6-ukhwpjnwzzzizek3pgr75zkbhxros5fq/lib/python3.11/site-packages/pandas/core/indexes/base.py:6249, in Index._raise_if_missing(self, key, indexer, axis_name)
6247 if nmissing:
6248 if nmissing == len(indexer):
-> 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
6252 raise KeyError(f"{not_found} not in index")
KeyError: "None of [Index(['latitude', 'longitude'], dtype='object')] are in the [columns]"
Changes proposed in this PR:
CalcDeltaImpact. Apparently, this is not covered by a test. I did not add one.PR Author Checklist
develop)PR Reviewer Checklist