How to solve KeyError: 0

Dear OEMOF users,

I sometimes run into a conspicuous error and I thought I would share the root of the problem in my case, so you can learn from it. The error comes up from your model definition:

  model = Model(energysystem)

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\_models.py:386 in __init__
    super().__init__(energysystem, **kwargs)

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\_models.py:162 in __init__
    self._construct()

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\_models.py:169 in _construct
    self._add_parent_block_variables()

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\_models.py:497 in _add_parent_block_variables
    if self.flows[o, i].fix[self.TIMESTEPS.at(1)] is not None:

  File ~\.conda\envs\energysystem\Lib\site-packages\pandas\core\frame.py:3893 in __getitem__
    indexer = self.columns.get_loc(key)

  File ~\.conda\envs\energysystem\Lib\site-packages\pandas\core\indexes\base.py:3798 in get_loc
    raise KeyError(key) from err

KeyError: 0

The issue has to do with that a fix that you apply to a flow has a type or length that OEMOF does not understand, it then spits out the above. Cheers!

Hi @TimKaasjager,

thanks for sharing. Could you please add some lines showing the construction of a Flow that will cause this error? It might help us catching the error in advance.

Cheers,
Patrik

Sure! I have pandas series with a datetime index and a data column. When i accidentally don’t specify the data when i set the pandas series as input argument (so subscripting ‘Load’ in inputs), I get the error:

data = pd.Series(index = date_range, data = data_array, name = 'Load')
# Make sure length is equal to number of model timesteps
load_data['irrigation'] = load_data['irrigation'][0:number_of_time_steps]

irrigation_load = cmp.Sink(
    label='irrigation',
    inputs={bel: flows.Flow(fix=data['Load'], nominal_value=1)}
)