iode.Variables.from_numpy

Variables.from_numpy(data: ndarray, vars_names: str | List[str] = None, first_period: str | Period = None, last_period: str | Period = None)[source]

Copy the numpy ndarray array into the IODE Variables database. A row of the ndarray represents a variable. A column of the ndarray represents a period.

Parameters:
data: numpy ndarray

Numpy ndarray containing the variables values to copy into the IODE Variables database. If the ndarray is a 1D array, either var_names must represent a single variable or first_period must be equal to last_period.

vars_names: str or list of str, optional

Names of the variables to copy into the IODE Variables database. Default to all variables names found in the present database.

first_period: str or Period, optional

First period of the values to copy into the IODE Variables database. Default to the first period of the present database.

last_period: str or Period, optional

Last period of the values to copy into the IODE Variables database. Default to the last period of the present database.

Warning

IODE and pandas don’t use the same constant to represent NaN values. When loading a pandas DataFrame into the Variables database, the pandas NaN values (\(nan\)) are converted to IODE NaN values (\(NA\)).

Examples

>>> from iode import variables, SAMPLE_DATA_DIR, Sample
>>> import numpy as np
>>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var")
Loading .../fun.var
394 objects loaded  
>>> # define the list of Variables to override, the first and last periods to copy
>>> vars_names = variables.get_names("A*")
>>> vars_names
['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC']
>>> first_period = "2000Y1"
>>> last_period = "2010Y1"
>>> sample = Sample(first_period, last_period)
>>> nb_periods = sample.nb_periods
>>> nb_periods
11
>>> # save original values to restore them later
>>> original_values = variables["A*", "2000Y1:2010Y1"].to_numpy()
>>> # create the numpy ndarray containing the values to copy into the Variables database
>>> data = np.zeros((len(vars_names), nb_periods), dtype=float)
>>> for i in range(len(vars_names)):
...     for j in range(nb_periods):
...         data[i, j] = i * nb_periods + j
>>> data
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.],
       [11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21.],
       [22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.],
       [33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43.],
       [44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54.]])
>>> variables["A*", "2000Y1:2010Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 2000Y1:2010Y1
mode: LEVEL

 name       2000Y1  2001Y1  2002Y1  2003Y1  2004Y1  2005Y1  2006Y1  2007Y1  2008Y1  2009Y1  2010Y1
ACAF         10.05    2.87   -0.93   -6.09  -14.58  -26.54  -28.99  -33.38  -38.41  -37.46  -37.83
ACAG        -41.53   18.94   19.98   21.02   22.07   23.11   24.13   25.16   26.19   27.23   28.25
AOUC          1.12    1.14    1.16    1.17    1.17    1.18    1.20    1.22    1.26    1.29    1.31
AOUC_         1.10    1.14    1.15    1.16    1.15    1.16    1.19    1.20    1.21    1.23    1.25
AQC           1.34    1.38    1.41    1.42    1.40    1.40    1.40    1.41    1.43    1.45    1.46

>>> # copy the numpy ndarray into the Variables database (overriding the existing values)
>>> variables.from_numpy(data, vars_names, first_period, last_period)
>>> variables["A*", "2000Y1:2010Y1"]
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 2000Y1:2010Y1
mode: LEVEL

 name       2000Y1  2001Y1  2002Y1  2003Y1  2004Y1  2005Y1  2006Y1  2007Y1  2008Y1  2009Y1  2010Y1
ACAF          0.00    1.00    2.00    3.00    4.00    5.00    6.00    7.00    8.00    9.00   10.00
ACAG         11.00   12.00   13.00   14.00   15.00   16.00   17.00   18.00   19.00   20.00   21.00
AOUC         22.00   23.00   24.00   25.00   26.00   27.00   28.00   29.00   30.00   31.00   32.00
AOUC_        33.00   34.00   35.00   36.00   37.00   38.00   39.00   40.00   41.00   42.00   43.00
AQC          44.00   45.00   46.00   47.00   48.00   49.00   50.00   51.00   52.00   53.00   54.00
>>> # if a subset represents all values to be updated, the values for the arguments 
>>> # vars_names, first_period and last_period can be omitted
>>> vars_subset = variables["A*", "2000Y1:2010Y1"]
>>> vars_subset.from_numpy(original_values)
>>> vars_subset
Workspace: Variables
nb variables: 5
filename: ...fun.var
description: Modèle fun - Simulation 1
sample: 2000Y1:2010Y1
mode: LEVEL

 name       2000Y1  2001Y1  2002Y1  2003Y1  2004Y1  2005Y1  2006Y1  2007Y1  2008Y1  2009Y1  2010Y1
ACAF         10.05    2.87   -0.93   -6.09  -14.58  -26.54  -28.99  -33.38  -38.41  -37.46  -37.83
ACAG        -41.53   18.94   19.98   21.02   22.07   23.11   24.13   25.16   26.19   27.23   28.25
AOUC          1.12    1.14    1.16    1.17    1.17    1.18    1.20    1.22    1.26    1.29    1.31
AOUC_         1.10    1.14    1.15    1.16    1.15    1.16    1.19    1.20    1.21    1.23    1.25
AQC           1.34    1.38    1.41    1.42    1.40    1.40    1.40    1.41    1.43    1.45    1.46