iode.Variables.__getitem__

Variables.__getitem__(key) float | Self[source]

Return the (subset of) variable(s) referenced by key.

The key represents a selection on the Variables names and optionally on the periods.

The selection on names can be:

  • a single Variable name (e.g. “ACAF”)

  • a list of Variable names (e.g. “ACAF;ACAG;AOUC”)

  • a pattern (e.g. “A*;*_”).

If the selection on names represents a list of names or of sub-patterns, each name or sub-pattern must be separated by a separator character which is either a whitespace ` , or a comma `,, or a semi-colon ;, or a tabulation t, or a newline n.

A (sub-)`pattern` is a list of characters representing a group of object names. It includes some special characters which have a special meaning:

  • * : any character sequence, even empty

  • ? : any character (one and only one)

  • @ : any alphanumerical char [A-Za-z0-9]

  • & : any non alphanumerical char

  • | : any alphanumeric character or none at the beginning and end of a string

  • ! : any non-alphanumeric character or none at the beginning and end of a string

  • `` : escape the next character

Note that the selection on names can contain references to IODE lists which are prefixed with the symbol $.

The selection on periods can be:

  • the whole sample (e.g. None)

  • a single period (e.g. “1990Y1”)

  • a range of periods (e.g. “1990Y1:2000Y1”)

  • a list of periods (e.g. [“1990Y1”, “1995Y1”, “2000Y1”])

Parameters:
key: str or list(str) or tuple(str, str) or tuple(str, list(str)) or tuple(str, str:str)

The key is split into two parts: the selection on names and the selection on periods. The selection on names can be a single name, a list of names, or a pattern. The selection on periods (optional) can be a single period, a list of periods, or a range of periods.

Returns:
float or Variables

If key represents a single name and a single period, the returned value is a float. If key represents a list of non-contiguous periods, the returned object is a pandas DataFrame. Otherwise, a subset of the Variables workspace is returned.

Examples

>>> from iode import SAMPLE_DATA_DIR
>>> from iode import variables, NA
>>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var")
Loading .../fun.var
394 objects loaded
>>> variables.sample
Sample("1960Y1:2015Y1")
>>> # -------- a) get one Variable --------
>>> # get the variable values for the whole sample
>>> variables["ACAF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1960Y1:2015Y1
mode: LEVEL

name        1960Y1  1961Y1  ...  2014Y1   2015Y1
ACAF            na      na  ...  -83.34   -96.41

>>> # get the variable value for a specific period
>>> variables["ACAF", "1990Y1"]
23.771
>>> # get the variable values for range of periods (using a Python slice)
>>> variables["ACAF", "1990Y1":"2000Y1"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:2000Y1
mode: LEVEL

name        1990Y1  1991Y1  1992Y1  1993Y1  1994Y1  1995Y1  1996Y1  1997Y1  1998Y1  1999Y1  2000Y1
ACAF         23.77   26.24   30.16   34.66    8.16  -13.13   32.17   39.94   29.65   13.53   10.05

>>> # same as above but with the colon ':' inside the periods range string
>>> variables["ACAF", "1990Y1:2000Y1"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:2000Y1
mode: LEVEL

name        1990Y1  1991Y1  1992Y1  1993Y1  1994Y1  1995Y1  1996Y1  1997Y1  1998Y1  1999Y1  2000Y1
ACAF         23.77   26.24   30.16   34.66    8.16  -13.13   32.17   39.94   29.65   13.53   10.05
>>> # b) -------- get the values for a single period --------
>>> variables[:, "1990Y1"]
Workspace: Variables
nb variables: 394
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:1990Y1
mode: LEVEL

 name       1990Y1
ACAF         23.77
ACAG        -28.17
AOUC          1.00
...            ...
ZKFO          1.00
ZX            0.00
ZZF_          0.69
 
>>> # or equivalently:
>>> variables["*", "1990Y1"]
Workspace: Variables
nb variables: 394
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:1990Y1
mode: LEVEL

 name       1990Y1
ACAF         23.77
ACAG        -28.17
AOUC          1.00
AOUC_         0.94
AQC           1.00
...            ...
ZJ            1.09
ZKF           0.81
ZKFO          1.00
ZX            0.00
ZZF_          0.69
>>> # c) -------- get a subset of the Variables database using a pattern --------
>>> vars_subset = variables["A*"]
>>> vars_subset.names
['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC']
>>> # get the variable values for a specific period -> Worspace subset
>>> variables["A*", "1990Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:1990Y1
mode: LEVEL

name       1990Y1
ACAF         23.77
ACAG        -28.17
AOUC          1.00
AOUC_         0.94
AQC           1.00

>>> # get the variable values for range of periods -> Workspace subset
>>> variables["A*", "1990Y1:2000Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:2000Y1
mode: LEVEL

 name       1990Y1  1991Y1  1992Y1  1993Y1  1994Y1  1995Y1  1996Y1  1997Y1  1998Y1  1999Y1  2000Y1
ACAF         23.77   26.24   30.16   34.66    8.16  -13.13   32.17   39.94   29.65   13.53   10.05
ACAG        -28.17  -30.93  -40.29  -43.16  -16.03  -41.85  -40.24  -32.93  -38.35  -39.86  -41.53
AOUC          1.00    1.02    1.03    1.03    1.05    1.05    1.05    1.08    1.09    1.11    1.12
AOUC_         0.94    0.96    0.97    0.98    0.99    1.00    1.00    1.03    1.04    1.08    1.10
AQC           1.00    1.06    1.11    1.15    1.16    1.16    1.16    1.20    1.20    1.34    1.34
>>> # d) -------- get a subset of the Variables database using a list of names --------
>>> vars_subset = variables[["ACAF", "ACAG", "AQC", "BQY", "BVY"]]
>>> vars_subset.names
['ACAF', 'ACAG', 'AQC', 'BQY', 'BVY']
>>> # get the variable values for a specific period -> pandas Series
>>> variables[["ACAF", "ACAG", "AQC", "BQY", "BVY"], "1990Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:1990Y1
mode: LEVEL

name        1990Y1
ACAF         23.77
ACAG        -28.17
AQC           1.00
BQY         -34.10
BVY         -34.10

>>> # get the variable values for a list of non-contiguous periods -> pandas DataFrame
>>> variables[["ACAF", "ACAG", "AQC", "BQY", "BVY"], ["1990Y1", "1995Y1", "2000Y1"]]
time          1990Y1      1995Y1      2000Y1
variables
ACAF       23.771000  -13.130997   10.046611
ACAG      -28.172186  -41.845993  -41.534787
AQC         1.000000    1.161687    1.338603
BQY       -34.099998   91.089355  119.955090
BVY       -34.099997  105.500000  150.053352       
>>> # get the variable values for range of periods -> Workspace subset
>>> variables[["ACAF", "ACAG", "AQC", "BQY", "BVY"], "1990Y1:2000Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1990Y1:2000Y1
mode: LEVEL

name        1990Y1  1991Y1  1992Y1  1993Y1  1994Y1  1995Y1  1996Y1  1997Y1  1998Y1  1999Y1  2000Y1
ACAF         23.77   26.24   30.16   34.66    8.16  -13.13   32.17   39.94   29.65   13.53   10.05
ACAG        -28.17  -30.93  -40.29  -43.16  -16.03  -41.85  -40.24  -32.93  -38.35  -39.86  -41.53
AQC           1.00    1.06    1.11    1.15    1.16    1.16    1.16    1.20    1.20    1.34    1.34
BQY         -34.10   -1.26  -13.75   52.16   66.63   91.09  104.68  113.52  116.19  117.91  119.96
BVY         -34.10   -1.30  -14.70   58.10   75.90  105.50  123.20  135.62  140.74  144.86  150.05