iode.Variables.__delitem__
- Variables.__delitem__(key)[source]
Remove the (subset of) variable(s) referenced by key from the Variables database.
- Parameters:
- key: str or list(str)
(list of) name(s) of the variable(s) to be removed. The list of names can be given as a string pattern (e.g. “A*;*_”).
Examples
>>> from iode import SAMPLE_DATA_DIR >>> from iode import variables >>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var") Loading .../fun.var 394 objects loaded
>>> # a) delete one variable >>> variables.get_names("A*") ['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC'] >>> del variables["ACAF"] >>> variables.get_names("A*") ['ACAG', 'AOUC', 'AOUC_', 'AQC']
>>> # b) delete several variables at once using a pattern >>> del variables["A*"] >>> variables.get_names("A*") []
>>> # c) delete several variables at once using a list of names >>> variables.get_names("B*") ['BENEF', 'BQY', 'BRUGP', 'BVY'] >>> del variables[["BENEF", "BQY"]] >>> variables.get_names("B*") ['BRUGP', 'BVY']
>>> # delete one variable from a subset of the global workspace >>> vars_subset = variables["D*"] >>> vars_subset.names ['DEBT', 'DPU', 'DPUF', 'DPUG', 'DPUGO', 'DPUH', 'DPUHO', 'DPUU', 'DTF', 'DTFX', 'DTH', 'DTH1', 'DTH1C', 'DTHX'] >>> del vars_subset["DPUGO"] >>> vars_subset.names ['DEBT', 'DPU', 'DPUF', 'DPUG', 'DPUH', 'DPUHO', 'DPUU', 'DTF', 'DTFX', 'DTH', 'DTH1', 'DTH1C', 'DTHX'] >>> # NOTE: the variable has also been deleted from the global workspace >>> "DPUGO" in variables False >>> variables.get_names("D*") ['DEBT', 'DPU', 'DPUF', 'DPUG', 'DPUH', 'DPUHO', 'DPUU', 'DTF', 'DTFX', 'DTH', 'DTH1', 'DTH1C', 'DTHX'] >>> # WARNING: when deleting (one) variable(s), the period(s) cannot be specified >>> del variables["DPUG", "1990Y1:1995Y1"] Traceback (most recent call last): ... RuntimeError: Cannot delete variable(s) 'DPUG'. The syntax 'del variables['DPUG']' must be used instead of 'del variables['DPUG', <periods>]'
>>> # subset over names and periods >>> vars_subset = variables["D*", "1990Y1:1995Y1"] >>> vars_subset.names ['DEBT', 'DPU', 'DPUF', 'DPUG', 'DPUH', 'DPUHO', 'DPUU', 'DTF', 'DTFX', 'DTH', 'DTH1', 'DTH1C', 'DTHX'] >>> del vars_subset["DPUG"] Traceback (most recent call last): ... RuntimeError: Cannot delete variable(s) 'DPUG' when the subset does not cover the whole sample of the IODE Variables workspace