iode.Variables.__itruediv__

Variables.__itruediv__(other)[source]

divide 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, divide 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

Dividing 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
>>> # divide 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     13.12   15.08   17.33    4.08   -6.57
ACAG    -15.47  -20.14  -21.58   -8.01  -20.92
AOUC      0.51    0.52    0.52    0.52    0.52
AOUC_     0.48    0.49    0.49    0.49    0.50
AQC       0.53    0.56    0.58    0.58    0.58
>>> # divide (a subset of) a Variables object by another
>>> 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      1.00    1.00    1.00    1.00    1.00
ACAG      1.00    1.00    1.00    1.00    1.00
AOUC      1.00    1.00    1.00    1.00    1.00
AOUC_     1.00    1.00    1.00    1.00    1.00
AQC       1.00    1.00    1.00    1.00    1.00
>>> # divide a a single variable by a pandas Series
>>> series = pd.Series([1.0, 0.5, 0.25, 0.2, 0.1], index=vars_subset.periods_as_str)
>>> series
1991Y1    1.00
1992Y1    0.50
1993Y1    0.25
1994Y1    0.20
1995Y1    0.10
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          1.00    2.00    4.00    5.00   10.00
>>> # divide a pandas Series to the subset corresponding to a single period
>>> series = pd.Series([1.0, 0.5, 0.25, 0.2, 0.1], index=vars_subset.names)
>>> series
ACAF     1.00
ACAG     0.50
AOUC     0.25
AOUC_    0.20
AQC      0.10
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         10.00
ACAG          2.00
AOUC          4.00
AOUC_         5.00
AQC          10.00
>>> # divide a (subset of a) Variables object by a pandas DataFrame  
>>> data = np.array([[0.1, 0.2, 0.25, 0.5, 1.0], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1]])
>>> df = pd.DataFrame(data, index=vars_subset.names, columns=vars_subset.periods_as_str) 
>>> df
       1991Y1  1992Y1  1993Y1  1994Y1  1995Y1
ACAF      0.1     0.2    0.25     0.5     1.0
ACAG      1.0     0.5    0.25     0.2     0.1
AOUC      1.0     0.5    0.25     0.2     0.1
AOUC_     1.0     0.5    0.25     0.2     0.1
AQC       1.0     0.5    0.25     0.2     0.1
>>> 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         10.00   10.00   16.00   10.00   10.00
ACAG          1.00    2.00    4.00    5.00   20.00
AOUC          1.00    2.00    4.00    5.00   40.00
AOUC_         1.00    2.00    4.00    5.00   50.00
AQC           1.00    2.00    4.00    5.00  100.00
>>> # divide (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       0.1     0.2    0.25     0.5     1.0
    ACAG       1.0     0.5    0.25     0.2     0.1
    AOUC       1.0     0.5    0.25     0.2     0.1
    AOUC_      1.0     0.5    0.25     0.2     0.1
    AQC        1.0     0.5    0.25     0.2     0.1
>>> 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        100.00   50.00   64.00   20.00    10.00
ACAG          1.00    4.00   16.00   25.00   200.00
AOUC          1.00    4.00   16.00   25.00   400.00
AOUC_         1.00    4.00   16.00   25.00   500.00
AQC           1.00    4.00   16.00   25.00  1000.00
      
>>> # WARNING: dividing 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).
>>> # divide a single variable by a numpy 1D ndarray
>>> data = np.array([1.0, 0.5, 1.0, 0.5, 1.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        100.00  100.00   64.00   40.00   10.00

>>> # divide 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          10.00
ACAG         400.00
AOUC         400.00
AOUC_       1000.00
AQC         1000.00

>>> # divide a (subset of a) Variables object by a numpy 2D ndarray
>>> data = np.array([[0.1, 0.2, 0.25, 0.5, 1.0], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1], 
...                  [1.0, 0.5, 0.25, 0.2, 0.1]])
>>> 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        1000.00  500.00  256.00   80.00     10.00
ACAG           1.00    8.00   64.00  125.00   4000.00
AOUC           1.00    8.00   64.00  125.00   4000.00
AOUC_          1.00    8.00   64.00  125.00  10000.00
AQC            1.00    8.00   64.00  125.00  10000.00