Using the datapackage-reader

%matplotlib inline
import os
import pandas as pd
import pkg_resources as pkg
import pprint

from pyomo.opt import SolverFactory
from oemof.solph import EnergySystem, Model
from oemof.tabular.facades import TYPEMAP
import as pp

from oemof.tabular import datapackage

Setting Path to Datapackage

The package comes with some example datapackage that you can use for testing. You can adapt this path to point to your datapackage. This scripts should work without any necessary additional changes.

name = "investment" # choose from ['dispatch', 'investment']

# path to directory with datapackage to load
datapackage_dir = pkg.resource_filename(
    "oemof.tabular", "examples/datapackages/{}".format(name)

The results path points to your home directory, a subdirectory oemof-results and the name of the datapackage specified above.

# create  path for results (we use the datapackage_dir to store results)
results_path = os.path.join(os.path.expanduser("~"), "oemof-results", name, "output")
if not os.path.exists(results_path):

Setting attributmap and typemap

The two arguments allow for adjusting the datapackage reader to your needs. The attribute map lets you specifiy to map column names of your datapackage resource (field names) to the facades. Take the following example:

The Load facade requires the argument amount. If you like (for whatever reason) to use a different naming in your csv-file like total_energy you can do this be specifying the following:

from oemof.tabular import facades as fc


attributemap = {
    fc.Load: {'amount', 'total_energy'}

So you can set the attribute map individually for all facades. However, we will use no mapping here for attributemap argument.

attributemap= {}

The typemap argument can be specified for your field (column name) type that specifcies which facade class should be used for instantiating the objects. We provide a default typemap. If you add your own facade classes, you must add these to typemap

    {'my_new_class': my_class}
typemap = TYPEMAP

# Look at current typemap
{'backpressure': <class 'oemof.tabular.facades.BackpressureTurbine'>,
 'bus': <class ''>,
 'commodity': <class 'oemof.tabular.facades.Commodity'>,
 'conversion': <class 'oemof.tabular.facades.Conversion'>,
 'dispatchable': <class 'oemof.tabular.facades.Dispatchable'>,
 'electrical bus': <class 'oemof.solph.custom.ElectricalBus'>,
 'electrical line': <class 'oemof.solph.custom.ElectricalLine'>,
 'excess': <class 'oemof.tabular.facades.Excess'>,
 'extraction': <class 'oemof.tabular.facades.ExtractionTurbine'>,
 'generator': <class 'oemof.tabular.facades.Generator'>,
 'link': <class 'oemof.tabular.facades.Link'>,
 'load': <class 'oemof.tabular.facades.Load'>,
 'reservoir': <class 'oemof.tabular.facades.Reservoir'>,
 'shortage': <class 'oemof.tabular.facades.Shortage'>,
 'storage': <class 'oemof.tabular.facades.Storage'>,
 'volatile': <class 'oemof.tabular.facades.Volatile'>}

Create EnergySystem from Datapackage

Using the .from_datapackge method, creating your EnergySystem is straight forward

# create energy system object
es = EnergySystem.from_datapackage(
    os.path.join(datapackage_dir, "datapackage.json"),
    {n.label: n for n in es.nodes}
UnresolvedFKError                         Traceback (most recent call last)
~/checkouts/ in deserialize_energy_system(cls, path, typemap, attributemap)
    293             try:
--> 294                 facade_data =, relations=True)
    295             except dp.exceptions.CastError:

~/checkouts/ in read(self, integrity, relations, foreign_keys_values, **options)
    211             integrity=integrity, relations=relations,
--> 212             foreign_keys_values=foreign_keys_values, **options)

~/checkouts/ in read(self, keyed, extended, cast, limit, integrity, relations, foreign_keys_values, exc_handler)
    248             exc_handler=exc_handler)
--> 249         for count, row in enumerate(rows, start=1):
    250             result.append(row)

~/checkouts/ in iter(self, keyed, extended, cast, integrity, relations, foreign_keys_values, exc_handler)
    183                                 row_number=row_number, row_data=keyed_row,
--> 184                                 error_data=local_keyed_values)
    185                             # If we reach this point we don't fail-early

~/checkouts/ in exc_handler(exc, *args, **kwargs)
     95                 stream.close()
---> 96                 raise exc

UnresolvedFKError: Foreign key "['profile']" violation in row "5": ('wind-profile',) not found in source_profile

During handling of the above exception, another exception occurred:

LoadError                                 Traceback (most recent call last)
<ipython-input-6-bc6e9eb2aad1> in <module>
      3     os.path.join(datapackage_dir, "datapackage.json"),
      4     attributemap=attributemap,
----> 5     typemap=typemap,
      6 )
      7 pprint.pprint(

~/checkouts/ in deserialize_energy_system(cls, path, typemap, attributemap)
    299                     ("Could not read data for resource with name `{}`. "
    300                      " Maybe wrong foreign keys?\n"
--> 301                      "Exception was: {}").format(, e))
    303             foreign_keys = {

LoadError: Could not read data for resource with name `source`.  Maybe wrong foreign keys?
Exception was: Foreign key "['profile']" violation in row "5": ('wind-profile',) not found in source_profile

Create Model

This again is straight forward and just the way you would use the oemof.solph package.

# check if the coin-branch-and-cut (cbc) solver library is available
cbc = SolverFactory('cbc').available()

if cbc:
    # create model from energy system (this is just oemof.solph)
    m = Model(es)

    # if you want dual variables / shadow prices uncomment line below
    # m.receive_duals()

    # select solver 'gurobi', 'cplex', 'glpk' etc

    # get the results from the the solved model(still oemof.solph)
    m.results = m.results()
WARNING: Could not locate the 'cbc' executable, which is required for solver

Writing Results

The nice thing about oemof.tabular.facades classes is their data model that allows for writing results in a standardized way. If you want more indidual result postprocessing, you can use all functionalities of the oemof.outputlib as the results object is a standard object.

if cbc:
    # now we use the write results method to write the results in oemof-tabular
    # format
    pp.write_results(m, results_path)

Postprocessing Results

if cbc:
    bus = 'bus0'
    path = os.path.join(results_path, ("").join([bus, '.csv']))
    df = pd.read_csv(path, index_col=0, parse_dates=True).loc[:, 'wind'].plot()