Fix KeyError retrieving marginal costs #651
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes the following KeyError:
KeyError Traceback (most recent call last)
Cell In[6], line 1
----> 1 etrago.adjust_network()
File ~/virtualenvs/hyBit/git_repos/eTraGo/etrago/tools/network.py:351, in Etrago.adjust_network(self)
347 self.geolocation_buses()
349 self.load_shedding()
--> 351 self.adjust_CH4_gen_carriers()
353 self.set_random_noise(0.01)
355 self.set_q_national_loads(cos_phi=0.9)
File
~/virtualenvs/hyBit/git_repos/eTraGo/etrago/tools/utilities.py:2635, in adjust_CH4_gen_carriers(self)
2629 df = pd.read_sql(sql, engine)
2630 # TODO: There might be a bug in here raising a KeyError. 2631 # If you encounter it, that means you have live data 2632 # to test against. Please do a git blame on these 2633 # lines and follow the hints in the commit message to 2634 # fix the bug.
-> 2635 marginal_cost = df["marginal_cost"]
2636 except sqlalchemy.exc.ProgrammingError:
2637 marginal_cost = marginal_cost_def
File
~/virtualenvs/hyBit/lib/python3.8/site-packages/pandas/core/series.py:981, in Series.getitem(self, key)
978 return self._values[key]
980 elif key_is_scalar:
--> 981 return self._get_value(key)
983 if is_hashable(key):
984 # Otherwise index.get_value will raise InvalidIndexError 985 try:
986 # For labels that don't resolve as scalars like tuples and frozensets
File
~/virtualenvs/hyBit/lib/python3.8/site-packages/pandas/core/series.py:1089, in Series._get_value(self, label, takeable)
1086 return self._values[label]
1088 # Similar to Index.get_value, but we do not fall back to positional -> 1089 loc = self.index.get_loc(label)
1090 return self.index._get_values_for_loc(self, loc, label)
File
~/virtualenvs/hyBit/lib/python3.8/site-packages/pandas/core/indexes/range.py:395, in RangeIndex.get_loc(self, key, method, tolerance) 393 raise KeyError(key) from err
394 self._check_indexing_error(key)
--> 395 raise KeyError(key)
396 return super().get_loc(key, method=method, tolerance=tolerance)
KeyError: 'marginal_cost'