iode.Variables.__radd__
- Variables.__radd__(other)[source]
Add other to the current (subset of) Variables object.
- Parameters:
- other: int, float, numpy ndarray, 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 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.
- Returns:
- Variables
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 a subset of a Variables object >>> new_vars_subset = 2.0 + vars_subset >>> new_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 two (subsets of) a Variables object >>> new_vars_subset = vars_subset + vars_subset >>> new_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
>>> # add an larray Array to a subset of a 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]]) >>> 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 >>> new_vars_subset = array + vars_subset >>> new_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 27.24 32.16 37.66 12.16 -8.13 ACAG -24.93 -33.29 -35.16 -7.03 -31.85 AOUC 12.02 13.03 14.03 15.05 16.05 AOUC_ 16.96 17.97 18.98 19.99 21.00 AQC 22.06 23.11 24.15 25.16 26.16
>>> # 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]) >>> updated_ACAF = data + vars_subset["ACAF"] >>> updated_ACAF array([27.240999 , 32.159 , 37.661999 , 12.1610022, -8.130997 ]) >>> # add a numpy 1D ndarray to the subset corresponding to a single period >>> vars_subset_1995Y1 = data + vars_subset[:, "1995Y1"] >>> vars_subset_1995Y1 array([[-12.130997 , -11.130997 , -10.130997 , -9.130997 , -8.130997 ], [-40.845993 , -39.845993 , -38.845993 , -37.845993 , -36.845993 ], [ 2.0498914 , 3.0498914 , 4.0498914 , 5.0498914 , 6.0498914 ], [ 1.99526324, 2.99526324, 3.99526324, 4.99526324, 5.99526324], [ 2.1616869 , 3.1616869 , 4.1616869 , 5.1616869 , 6.1616869 ]]) >>> # add a numpy 2D ndarray to a (subset of a) 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]]) >>> new_vars_subset = data + vars_subset >>> new_vars_subset array([[ 27.240999 , 32.159 , 37.661999 , 12.1610022 , -8.130997 ], [-24.934 , -33.285999 , -35.157997 , -7.029003 , -31.845993 ], [ 12.02443339, 13.0314501 , 14.03091768, 15.04628419, 16.0498914 ], [ 16.96466659, 17.97403904, 18.97881286, 19.98955638, 20.99526324], [ 22.0628064 , 23.1102825 , 24.1532652 , 25.1571276 , 26.1616869 ]])