iode.Variables.__iadd__

Variables.__iadd__(other)[source]

Add other to the current (subset of) Variables object.

Parameters:
other: int, float, numpy ndarray, pandas Series, pandas DataFrame, larray Array or iode Variables

If other is an int or a float, add the scalar to all values of the current (subset of) Variables object. If other is a numpy ndarray, the shape of the ndarray must be compatible with the current (subset of) Variables object. Specifically, the number of rows must be equal to the number of variables and the number of columns must be equal to the number of periods. If other is a pandas Series, it must represent either a single variable or a single period. If other is a pandas DataFrame, it must represent the same variables names and periods as the current (subset of) Variables object. Specifically, the index of the DataFrame must be equal to the variables names and the columns of the DataFrame must be equal to the periods. If other is an larray Array, its last axis must be equal to the periods and be named ‘time’. If the Array has more than two axes, the first n-1 axes are combined to form the variables names. The first (combined) axis must be equal to the variables names. If other is an iode Variables object, add the two Variables objects. self and other must share the same sample and represent the same set of variables names.

Warning

Adding a numpy ndarray to a Variables object is not recommended as there is no compatibility check between for the names and periods. The result is not guaranteed to be the one you expected. This possibility is provided for speed reasons (when the database or the subset is large).

Examples

>>> import numpy as np
>>> import pandas as pd
>>> import larray as la
>>> from iode import SAMPLE_DATA_DIR
>>> from iode import variables, NA, Sample
>>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var")
Loading .../fun.var
394 objects loaded
>>> vars_subset = variables["A*", "1991Y1:1995Y1"]
>>> vars_subset.names
['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC']
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         26.24   30.16   34.66    8.16  -13.13
ACAG        -30.93  -40.29  -43.16  -16.03  -41.85
AOUC          1.02    1.03    1.03    1.05    1.05
AOUC_         0.96    0.97    0.98    0.99    1.00
AQC           1.06    1.11    1.15    1.16    1.16
>>> # add a scalar to all values of the current subset of a Variables object
>>> vars_subset += 2.0
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         28.24   32.16   36.66   10.16  -11.13
ACAG        -28.93  -38.29  -41.16  -14.03  -39.85
AOUC          3.02    3.03    3.03    3.05    3.05
AOUC_         2.96    2.97    2.98    2.99    3.00
AQC           3.06    3.11    3.15    3.16    3.16
>>> # add a (subsets of) a Variables object to the current (subset of) Variables object
>>> vars_subset += vars_subset
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         56.48   64.32   73.32   20.32  -22.26
ACAG        -57.87  -76.57  -82.32  -28.06  -79.69
AOUC          6.05    6.06    6.06    6.09    6.10
AOUC_         5.93    5.95    5.96    5.98    5.99
AQC           6.13    6.22    6.31    6.31    6.32
>>> # add a pandas Series to a single variable
>>> series = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0], index=vars_subset.periods_as_str)
>>> series
1991Y1    1.0
1992Y1    2.0
1993Y1    3.0
1994Y1    4.0
1995Y1    5.0
dtype: float64
>>> vars_subset["ACAF"] += series
>>> vars_subset["ACAF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         57.48   66.32   76.32   24.32  -17.26
>>> # add a pandas Series to the subset corresponding to a single period
>>> series = pd.Series([1.0, 2.0, 3.0, 4.0, 5.0], index=vars_subset.names)
>>> series
ACAF     1.0
ACAG     2.0
AOUC     3.0
AOUC_    4.0
AQC      5.0
dtype: float64
>>> vars_subset[:, "1995Y1"] += series
>>> vars_subset[:, "1995Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1995Y1:1995Y1
mode: LEVEL

 name       1995Y1
ACAF        -16.26
ACAG        -77.69
AOUC          9.10
AOUC_         9.99
AQC          11.32
>>> # add a pandas DataFrame to the current subset of the Variables object  
>>> data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0], 
...                  [6.0, 7.0, 8.0, 9.0, 10.0], 
...                  [11.0, 12.0, 13.0, 14.0, 15.0], 
...                  [16.0, 17.0, 18.0, 19.0, 20.0], 
...                  [21.0, 22.0, 23.0, 24.0, 25.0]],)
>>> df = pd.DataFrame(data, index=vars_subset.names, columns=vars_subset.periods_as_str) 
>>> df
       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF      1.0     2.0     3.0     4.0     5.0
ACAG      6.0     7.0     8.0     9.0    10.0
AOUC     11.0    12.0    13.0    14.0    15.0
AOUC_    16.0    17.0    18.0    19.0    20.0
AQC      21.0    22.0    23.0    24.0    25.0
>>> vars_subset += df
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         58.48   68.32   79.32   28.32  -11.26
ACAG        -51.87  -69.57  -74.32  -19.06  -67.69
AOUC         17.05   18.06   19.06   20.09   24.10
AOUC_        21.93   22.95   23.96   24.98   29.99
AQC          27.13   28.22   29.31   30.31   36.32
>>> # add an larray Array to the current subset of the Variables object
>>> axis_names = la.Axis(name="names", labels=vars_subset.names)
>>> axis_time = la.Axis(name="time", labels=vars_subset.periods_as_str)
>>> array = la.Array(data, axes=(axis_names, axis_time))
>>> array
names\time  1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
      ACAF     1.0     2.0     3.0     4.0     5.0
      ACAG     6.0     7.0     8.0     9.0    10.0
      AOUC    11.0    12.0    13.0    14.0    15.0
     AOUC_    16.0    17.0    18.0    19.0    20.0
       AQC    21.0    22.0    23.0    24.0    25.0
>>> vars_subset += array
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         59.48   70.32   82.32   32.32   -6.26
ACAG        -45.87  -62.57  -66.32  -10.06  -57.69
AOUC         28.05   30.06   32.06   34.09   39.10
AOUC_        37.93   39.95   41.96   43.98   49.99
AQC          48.13   50.22   52.31   54.31   61.32
        
>>> # WARNING: adding a numpy ndarray to a (subset of a) Variables object is not recommended 
>>> #          as there is no compatibility check between for the names and periods.
>>> #          The result is not guaranteed to be the one you expected.
>>> #          This possibility is provided for speed reasons 
>>> #          (when dealing with large subsets/databases).
>>> # add a numpy 1D ndarray to a single variable
>>> data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
>>> vars_subset["ACAF"] += data
>>> vars_subset["ACAF"]
Workspace: Variables
nb variables: 1
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         60.48   72.32   85.32   36.32   -1.26

>>> # add a numpy 1D ndarray to the subset corresponding to a single period
>>> vars_subset[:, "1995Y1"] += data
>>> vars_subset[:, "1995Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1995Y1:1995Y1
mode: LEVEL

 name       1995Y1
ACAF         -0.26
ACAG        -55.69
AOUC         42.10
AOUC_        53.99
AQC          66.32
        
>>> # add a numpy 2D ndarray to the current (subset of the) Variables object
>>> data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0], 
...                  [6.0, 7.0, 8.0, 9.0, 10.0], 
...                  [11.0, 12.0, 13.0, 14.0, 15.0], 
...                  [16.0, 17.0, 18.0, 19.0, 20.0], 
...                  [21.0, 22.0, 23.0, 24.0, 25.0]])
>>> vars_subset += data
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 1991Y1:1995Y1
mode: LEVEL

 name       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF         61.48   74.32   88.32   40.32    4.74
ACAG        -39.87  -55.57  -58.32   -1.06  -45.69
AOUC         39.05   42.06   45.06   48.09   57.10
AOUC_        53.93   56.95   59.96   62.98   73.99
AQC          69.13   72.22   75.31   78.31   91.32