Example use with random data¶
Here, we will load test MRIO data. We have defined a .yaml file with all the require instructions, to load the data.
[1]:
import os
import logging
import mrio_toolbox as mrio
# get the current workbook directory
workbookDir = os.getcwd()
# Load the data
test = mrio.MRIO(file="loading_instructions.yaml")
All raw data tables will be stored in an mrio object, which offers several functions for indexing and manipulating the data.
First, let’s check which countries and sectors are in our mrio object.
[2]:
print(test.labels["countries"])
print(test.labels["sectors"])
['Austria', 'Belgium', 'Bulgaria', 'Cyprus', 'Czech Republic', 'ROW']
['Agriculture', 'Manufacturing', 'Services']
Our MRIO object contains an inter-industry table, noted t:
[3]:
print(test.parts)
{'t': <mrio_toolbox._parts._Part.Part object at 0x7f3a065c5be0>}
This where the actual data is stored. We can check its dimensions and its shape
[4]:
print(test.t.get_dimensions())
print(test.t.shape)
[['countries', 'sectors'], ['countries', 'sectors']]
(18, 18)
Now, we can look at the exports from Czech republic to Austria. (Note that this is all random data!)
In order to nicely print our outputs, we can transform it into a Pandas DataFrame.
[5]:
cz_exports = test.t[["Czech Republic"],["Austria"]]
cz_exports.to_pandas()
[5]:
| countries | Austria | |||
|---|---|---|---|---|
| sectors | Agriculture | Manufacturing | Services | |
| countries | sectors | |||
| Czech Republic | Agriculture | 8 | 38 | 36 |
| Manufacturing | 96 | 48 | 31 | |
| Services | 70 | 10 | 75 | |
In the groupings.yaml file, we have defined groupings. We have grouped the EU countries together and put our RoW “country” into a RoW group.
We can use those groupings to look at agricultural exports of the EU group to the rest of the world.
[6]:
test.t.aggregate(on = "countries")
[6]:
<mrio_toolbox._parts._Part.Part at 0x7f3a065e1230>
[7]:
test.t[["EU","Agriculture"],"ROW"].to_pandas()
[7]:
| countries | ROW | |||
|---|---|---|---|---|
| sectors | Agriculture | Manufacturing | Services | |
| countries | sectors | |||
| Austria | Agriculture | 5 | 36 | 95 |
| Belgium | Agriculture | 84 | 24 | 77 |
| Bulgaria | Agriculture | 58 | 47 | 21 |
| Cyprus | Agriculture | 11 | 86 | 95 |
| Czech Republic | Agriculture | 56 | 52 | 57 |
We can also output a simplified table, where all EU countries are grouped together:
[8]:
test.t.aggregate(on="countries").to_pandas()
[8]:
| countries | EU | ROW | |||||
|---|---|---|---|---|---|---|---|
| sectors | Agriculture | Manufacturing | Services | Agriculture | Manufacturing | Services | |
| countries | sectors | ||||||
| EU | Agriculture | 1142.0 | 1191.0 | 1333.0 | 214.0 | 245.0 | 345.0 |
| Manufacturing | 1340.0 | 1253.0 | 1363.0 | 248.0 | 279.0 | 304.0 | |
| Services | 1140.0 | 1290.0 | 1446.0 | 288.0 | 209.0 | 167.0 | |
| ROW | Agriculture | 235.0 | 251.0 | 267.0 | 12.0 | 40.0 | 97.0 |
| Manufacturing | 267.0 | 275.0 | 271.0 | 77.0 | 15.0 | 63.0 | |
| Services | 220.0 | 321.0 | 300.0 | 34.0 | 35.0 | 62.0 | |
Now, we want to remove intra-sectoral trade. The “extract” method will do the job.
[9]:
extracted = test.t.extraction(dimensions="sectors",domestic_only=True)
extracted[[("Austria","Belgium"),"all"],[("Austria","Belgium"),"all"]].to_pandas()
[9]:
| countries | Austria | Belgium | |||||
|---|---|---|---|---|---|---|---|
| sectors | Agriculture | Manufacturing | Services | Agriculture | Manufacturing | Services | |
| countries | sectors | ||||||
| Austria | Agriculture | 0 | 4 | 29 | 0 | 14 | 80 |
| Manufacturing | 30 | 0 | 63 | 35 | 0 | 2 | |
| Services | 49 | 60 | 0 | 5 | 60 | 0 | |
| Belgium | Agriculture | 0 | 94 | 94 | 0 | 31 | 11 |
| Manufacturing | 91 | 0 | 83 | 21 | 0 | 86 | |
| Services | 32 | 9 | 0 | 13 | 46 | 0 | |
[10]:
test.groupings
[10]:
{'countries': {'EU': ['Austria',
'Belgium',
'Bulgaria',
'Cyprus',
'Czech Republic'],
'ROW': ['ROW']},
'sectors': {'primary': ['Agriculture'],
'secondary': ['Manufacturing'],
'tertiary': ['Services']}}
In the previous prompt, I added the “all” keyword. This makes sure I select Austria and Belgium on the “countries” axis; and “all” sectors on the “sectors” axis.
Note that the package does not support numpy-like ellipsis (:), nor slice objects.