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\ in __init__
    super().__init__(energysystem, **kwargs)

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\ in __init__

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\ in _construct

  File ~\.conda\envs\energysystem\Lib\site-packages\oemof\solph\ in _add_parent_block_variables
    if self.flows[o, i].fix[] is not None:

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

  File ~\.conda\envs\energysystem\Lib\site-packages\pandas\core\indexes\ 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.


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(
    inputs={bel: flows.Flow(fix=data['Load'], nominal_value=1)}