Pandas get_loc method raises "KeyError: Year", if MUSE foresight != 5

55 views
Skip to first unread message

jamshid

unread,
Nov 18, 2023, 11:38:15 AM11/18/23
to MUSE
Dear Sara and MUSE team,

I get KeyError: Year, while MUSE "foresight" is not equal to 5.
Error raises from "get_loc" method of pandas, when trying to get integer location.
It can be replicated using "default_timeslice" folder with the following values in the toml file.
time_framework = [2020, 2026, 2032, ...]
foresight = 6  

In this case the KeyError will be 2021, if you will try a different value (except 5) for foresight you will get "another year" in the error.

I'm using pandas==1.3.0 and xarray==2022.3.0
Below is the error message.

Traceback (most recent call last):
  File "...\.venv\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 20..
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "...\my_code.py", line 30, in <module>
    run_muse()
  File "...\.venv\lib\site-packages\muse\mca.py", line 364, in run
    self.outputs(
  File "...\.venv\lib\site-packages\muse\outputs\sector.py", line 106, in save_multiple_outputs
    return [
  File "...\.venv\lib\site-packages\muse\outputs\sector.py", line 107, in <listcomp>
    sink(quantity(market, *args), year=year)
  File "...\.venv\lib\site-packages\muse\registration.py", line 172, in decorated
    result = inner_decorated(*args, **kwargs)
  File "...\.venv\lib\site-packages\muse\outputs\mca.py", line 67, in decorated
    result = function(*args, **kwargs)
  File "...\.venv\lib\site-packages\muse\outputs\mca.py", line 344, in metric_supply
    return _aggregate_sectors(sectors, market_out, op=sector_supply)
  File "...\.venv\lib\site-packages\muse\outputs\mca.py", line 250, in _aggregate_sectors
    alldata = [op(sector, *args) for sector in sectors]
  File "...\.venv\lib\site-packages\muse\outputs\mca.py", line 250, in <listcomp>
    alldata = [op(sector, *args) for sector in sectors]
  File "...\.venv\lib\site-packages\muse\outputs\mca.py", line 360, in sector_supply
    agent_market = market.sel(year=output_year).copy()
  File "...\.venv\lib\site-packages\xarray\core\dataset.py", line 2501, in sel
    pos_indexers, new_indexes = remap_label_indexers(
  File "...\.venv\lib\site-packages\xarray\core\coordinates.py", line 421, in remap_label_indexers
    pos_indexers, new_indexes = indexing.remap_label_indexers(
  File "...\.venv\lib\site-packages\xarray\core\indexing.py", line 121, in remap_label_indexers
    idxr, new_idx = index.query(labels, method=method, tolerance=tolerance)
  File "...\.venv\lib\site-packages\xarray\core\indexes.py", line 241, in query
    indexer = self.index.get_loc(label_value)
  File "...\.venv\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: 20..


Regards,
Jamshid

Diego Moya

unread,
Nov 19, 2023, 7:29:25 AM11/19/23
to MUSE
Thanks,  Jamshid.

I just got a similar error. I attach it below.
Hope, the MUSE team can read this soon.

Best regards, 

Diego

Error:
...site-packages\pandas\core\indexes\multi.py", line 3211, in _get_level_indexer
    raise KeyError(key)
KeyError: 2010

sgiarols

unread,
Nov 20, 2023, 11:34:49 AM11/20/23
to MUSE
Dear all

you would need to specify
forecast = 6  # Optional, defaults to 5
in every sector subsector (i.e. residential, power, gas).

Best

Diego Moya

unread,
Jan 17, 2024, 6:01:22 AM1/17/24
to MUSE
Dear all, 

I got this error again in the last version. 
Jamshid, did you manage to solve this issue?

Best regards, 

Diego

jamshid

unread,
Jan 24, 2024, 5:55:37 AM1/24/24
to MUSE
Dear Diego, 

In my case error came from  ...\muse\outputs\mca.py  @register_output_quantity hook
Solution in my case:
Please, check "value" for quantity of [[outputs]] in the toml file.
Some quantity of outputs will not work if foresight != 5.
Below, you can find outputs which will work with any foresight value.

[[outputs]]
quantity = "consumption"
...
[[outputs]]
quantity = "supply"
...
[[outputs]]
quantity = "prices"
...
[[outputs]]
quantity = "capacity"
...
[[outputs]]
quantity = "capital_costs"

You can not use other values (i.e., quantity = "LCOE") if foresight != 5 

Regards,
Jamshid
Reply all
Reply to author
Forward
0 new messages