iode.Variables.from_frame

Variables.from_frame(df: DataFrame)[source]

Copy the pandas DataFrame df into the IODE Variables database. The variable names to copy are deduced from the index of the DataFrame. The column names must match the sub-periods of the current Variables sample. The column names can be of type string or int.

Parameters:
df: DataFrame

pandas DataFrame containing the variables to copy into the IODE Variables 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\)).

Notes

The index of the passed DataFrame is sorted in alphabetical order before copying to IODE Variables database.

Examples

>>> from iode import variables
>>> import numpy as np
>>> import pandas as pd
>>> variables.clear()
>>> len(variables)
0
  1. Filling an empty Variables database from a pandas DataFrame

>>> # create the pandas DataFrame
>>> vars_names = [f"{region}_{code}" for region in ["VLA", "WAL", "BXL"] for code in ["00", "01", "02"]]
>>> periods_list = [f"{i}Y1" for i in range(1960, 1971)]
>>> nb_periods = len(periods_list)
>>> 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
>>> df = pd.DataFrame(index=vars_names, columns=periods_list, data=data)
>>> # display the dataframe
>>> df
        1960Y1  1961Y1  1962Y1  1963Y1  ...  1967Y1  1968Y1  1969Y1  1970Y1
VLA_00     0.0     1.0     2.0     3.0  ...     7.0     8.0     9.0    10.0
VLA_01    11.0    12.0    13.0    14.0  ...    18.0    19.0    20.0    21.0
VLA_02    22.0    23.0    24.0    25.0  ...    29.0    30.0    31.0    32.0
WAL_00    33.0    34.0    35.0    36.0  ...    40.0    41.0    42.0    43.0
WAL_01    44.0    45.0    46.0    47.0  ...    51.0    52.0    53.0    54.0
WAL_02    55.0    56.0    57.0    58.0  ...    62.0    63.0    64.0    65.0
BXL_00    66.0    67.0    68.0    69.0  ...    73.0    74.0    75.0    76.0
BXL_01    77.0    78.0    79.0    80.0  ...    84.0    85.0    86.0    87.0
BXL_02    88.0    89.0    90.0    91.0  ...    95.0    96.0    97.0    98.0

[9 rows x 11 columns]
>>> # load into the IODE Variables database
>>> variables.from_frame(df)
>>> len(variables)
9
>>> variables.names
['BXL_00', 'BXL_01', 'BXL_02', 'VLA_00', 'VLA_01', 'VLA_02', 'WAL_00', 'WAL_01', 'WAL_02']
>>> variables.sample
Sample("1960Y1:1970Y1")
>>> variables["VLA_00"]
Workspace: Variables
nb variables: 1
filename: ws
sample: 1960Y1:1970Y1
mode: LEVEL

 name       1960Y1  1961Y1  1962Y1  ...  1968Y1  1969Y1  1970Y1
VLA_00        0.00    1.00    2.00  ...    8.00    9.00   10.00

>>> variables["BXL_02"]
Workspace: Variables
nb variables: 1
filename: ws
sample: 1960Y1:1970Y1
mode: LEVEL

 name       1960Y1  1961Y1  1962Y1  ...  1968Y1  1969Y1  1970Y1
BXL_02       88.00   89.00   90.00  ...   96.00   97.00   98.00
  1. Updating an existing Variables database from a pandas DataFrame

>>> # take a subset of the pandas DataFrame
>>> df = df.iloc[6:, 2:-2]
>>> # change values
>>> df += 3.0
>>> # add a new entry
>>> df.loc['BXL_03'] = [104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0]
>>> df
        1962Y1  1963Y1  ...  1967Y1  1968Y1
BXL_00    71.0    72.0  ...    76.0    77.0
BXL_01    82.0    83.0  ...    87.0    88.0
BXL_02    93.0    94.0  ...    98.0    99.0
BXL_03   104.0   105.0  ...   109.0   110.0
>>> # update the IODE Variables database
>>> variables.from_frame(df)
>>> len(variables)
10
>>> variables.names
['BXL_00', 'BXL_01', 'BXL_02', 'BXL_03', ..., 'WAL_00', 'WAL_01', 'WAL_02']
>>> # note that the new variable BXL_03 has been added with NA values 
>>> # for the periods present in the Variables sample but not in the DataFrame
>>> variables
Workspace: Variables
nb variables: 10
filename: ws
sample: 1960Y1:1970Y1
mode: LEVEL

 name      1960Y1  1961Y1  1962Y1  1963Y1  1964Y1  1965Y1  1966Y1  1967Y1  1968Y1  1969Y1  1970Y1        
BXL_00      66.00   67.00   71.00   72.00   73.00   74.00   75.00   76.00   77.00   75.00   76.00        
BXL_01      77.00   78.00   82.00   83.00   84.00   85.00   86.00   87.00   88.00   86.00   87.00        
BXL_02      88.00   89.00   93.00   94.00   95.00   96.00   97.00   98.00   99.00   97.00   98.00        
BXL_03         na      na  104.00  105.00  106.00  107.00  108.00  109.00  110.00      na      na        
VLA_00       0.00    1.00    2.00    3.00    4.00    5.00    6.00    7.00    8.00    9.00   10.00        
VLA_01      11.00   12.00   13.00   14.00   15.00   16.00   17.00   18.00   19.00   20.00   21.00        
VLA_02      22.00   23.00   24.00   25.00   26.00   27.00   28.00   29.00   30.00   31.00   32.00        
WAL_00      33.00   34.00   35.00   36.00   37.00   38.00   39.00   40.00   41.00   42.00   43.00        
WAL_01      44.00   45.00   46.00   47.00   48.00   49.00   50.00   51.00   52.00   53.00   54.00        
WAL_02      55.00   56.00   57.00   58.00   59.00   60.00   61.00   62.00   63.00   64.00   65.00