iode.Variables.__imul__

Variables.__imul__(other)[source]

multiply the current (subset of) Variables object by other.

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

If other is an int or a float, multiply all values of the current (subset of) Variables object by the scalar. 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, it must share the same sample and represent the same set of variables names as self.

Warning

Multiplying 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
>>> # multiply all values of a subset of a Variables object by a scalar
>>> 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             52.48   60.32   69.32   16.32  -26.26
ACAG            -61.87  -80.57  -86.32  -32.06  -83.69
AOUC          2.05        2.06    2.06    2.09    2.10
AOUC_         1.93        1.95    1.96    1.98    1.99
AQC           2.13        2.22    2.31    2.31    2.32
>>> # multiply two (subsets of) a 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        2754.36 3638.26 4805.82  266.41  689.69
ACAG        3827.65 6491.85 7450.45 1027.72 7004.35
AOUC           4.20    4.26    4.25    4.38    4.41
AOUC_          3.72    3.80    3.83    3.92    3.96
AQC            4.52    4.93    5.32    5.36    5.40
>>> # multiply a single variable by a pandas Series
>>> 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        2754.36  7276.52  14417.45  1065.63  3448.46
>>> # multiply a subset corresponding to a single period by a pandas Series
>>> 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         3448.46
ACAG        14008.70
AOUC           13.23
AOUC_          15.85
AQC            26.99
>>> # multiply a subset of a Variables object by a pandas DataFrame  
>>> 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         2754.36        14553.04        43252.35        4262.53   17242.31
ACAG        22965.90        45442.93        59603.61        9249.44  140086.97
AOUC           46.18           51.07           55.27          61.30     198.41
AOUC_          59.56           64.52           68.98          74.42     316.98
AQC            94.88          108.48          122.36         128.54     674.76
>>> # multiply a subset of a Variables object by an larray Array
>>> 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          2754.36        29106.09       129757.05       17050.10          86211.54
ACAG        137795.38       318100.50       476828.85       83244.98        1400869.70
AOUC           507.94          612.80          718.45         858.25           2976.13
AOUC_          952.92         1096.76         1241.66        1414.00           6339.51
AQC           1992.54         2386.56         2814.32        3084.93          16868.96
    
>>> # WARNING: multiplying 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).
>>> # multiply a single variable by a numpy 1D ndarray
>>> 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        2754.36  58212.18  389271.15  68200.40  431057.71

>>> # multiply the subset corresponding to a single period by a numpy 1D ndarray
>>> 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         431057.71
ACAG        2801739.41
AOUC           8928.40
AOUC_         25358.05
AQC           84344.78
      
>>> # multiply a (subset of a) Variables object by a numpy 2D ndarray
>>> 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          2754.36        116424.36      1167813.46      272801.62        2155288.53
ACAG        826772.28       2226703.47      3814630.82      749204.78       28017394.08
AOUC          5587.35          7353.60         9339.81       12015.54         133926.04
AOUC_        15246.65         18644.88        22349.96       26865.93         507161.04
AQC          41843.33         52504.32        64729.44       74038.26        2108619.46