iode.Variables.from_frame
- Variables.from_frame(self, df: DataFrame)
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.
- 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\)).
See also
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
>>> # 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)] >>> data = np.arange(len(vars_names) * len(periods_list), dtype=float).reshape(len(vars_names), len(periods_list)) >>> 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 '1960Y1:1970Y1' >>> variables["VLA_00"] [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] >>> variables["BXL_02"] [88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0]