Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError retrieving marginal costs #651

Merged
merged 1 commit into from
Sep 10, 2023
Merged

Conversation

birgits
Copy link
Contributor

@birgits birgits commented Sep 7, 2023

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'

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'
@birgits birgits requested review from gnn and CarlosEpia September 7, 2023 16:20
@birgits
Copy link
Contributor Author

birgits commented Sep 7, 2023

@gnn I added you because you anticipated this error and wrote a nice commit message that helped me fix it :)

5ebc9cb

Copy link
Contributor

@CarlosEpia CarlosEpia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and it runs successfully until the LOPF.

@birgits birgits merged commit f58ecf5 into dev Sep 10, 2023
6 checks passed
@birgits birgits deleted the bugfix/adjust_ch4_gen_carriers branch September 10, 2023 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants