CodeList

class nomenclature.codelist.CodeList(*, name, mapping={})[source]

A class for nomenclature codelists & attributes

Attributes:
namestr

Name of the CodeList

mappingdict

Dictionary of Code objects

Methods

filter(**kwargs)

Filter a CodeList by any attribute-value pairs.

from_directory(name, path[, config, ...])

Initialize a CodeList from a directory with codelist files

read_excel(name, source, sheet_name, col[, ...])

Parses an xlsx file with a codelist

to_csv([path, sort_by_code])

Write the codelist to a comma-separated values (csv) file

to_excel(excel_writer[, sheet_name, ...])

Write the codelist to an Excel spreadsheet

to_pandas([sort_by_code])

Export the CodeList to a pandas.DataFrame

to_yaml([path])

Write mapping to yaml file or return as stream

validate_items(items)

Validate that a list of items are valid codes

filter(**kwargs)[source]

Filter a CodeList by any attribute-value pairs.

Parameters:
**kwargs

Attribute-value mappings to be used for filtering.

Returns:
CodeList

CodeList with Codes that match attribute-value pairs.

classmethod from_directory(name, path, config=None, file_glob_pattern='**/*')[source]

Initialize a CodeList from a directory with codelist files

Parameters:
namestr

Name of the CodeList

pathpathlib.Path or path-like

Directory with the codelist files

config: :class:`NomenclatureConfig`, optional

Attributes for configuring the CodeList

file_glob_patternstr, optional

Pattern to downselect codelist files by name

Returns:
instance of cls (CodeList if not inherited)
classmethod read_excel(name, source, sheet_name, col, attrs=None)[source]

Parses an xlsx file with a codelist

Parameters:
namestr

Name of the CodeList

sourcestr, path, file-like object

Path to Excel file with definitions (codelists).

sheet_namestr

Sheet name of source.

colstr

Column from sheet_name to use as codes.

attrslist, optional

Columns from sheet_name to use as attributes.

to_csv(path=None, sort_by_code=False, **kwargs)[source]

Write the codelist to a comma-separated values (csv) file

Parameters:
pathstr, path or file-like, optional

File path as string or pathlib.Path, or file-like object. If None, the result is returned as a csv-formatted string. See pandas.DataFrame.to_csv() for details.

sort_by_codebool, optional

Sort the codelist before exporting to csv.

**kwargs

Passed to pandas.DataFrame.to_csv().

Returns:
None or csv-formatted string (if path is None)
to_excel(excel_writer, sheet_name=None, sort_by_code=False, **kwargs)[source]

Write the codelist to an Excel spreadsheet

Parameters:
excel_writerpath-like, file-like, or ExcelWriter object

File path as string or pathlib.Path, or existing pandas.ExcelWriter.

sheet_namestr, optional

Name of sheet that will have the codelist. If None, use the codelist name.

sort_by_codebool, optional

Sort the codelist before exporting to file.

**kwargs

Passed to pandas.ExcelWriter (if excel_writer is path-like).

to_pandas(sort_by_code=False)[source]

Export the CodeList to a pandas.DataFrame

Parameters:
sort_by_codebool, optional

Sort the codelist before exporting to csv.

to_yaml(path=None)[source]

Write mapping to yaml file or return as stream

Parameters:
pathpathlib.Path or str, optional

Write to file path if not None, otherwise return as stream

validate_items(items)[source]

Validate that a list of items are valid codes

Returns:
list

Returns the list of items that are not defined in the codelist

class nomenclature.codelist.VariableCodeList(*, name, mapping={})[source]

A subclass of CodeList specified for variables

Attributes:
namestr

Name of the VariableCodeList

mappingdict

Dictionary of VariableCode objects

Methods

cast_variable_components_args(v)

Cast "components" list of dicts to a codelist

check_variable_region_aggregation_args(v)

Check that any variable "region-aggregation" mappings are valid

check_weight_in_vars(v)

Check that all variables specified in 'weight' are present in the codelist

vars_default_args(variables)

return subset of variables which does not feature any special pyam aggregation arguments and where skip_region_aggregation is False

classmethod cast_variable_components_args(v)[source]

Cast “components” list of dicts to a codelist

classmethod check_variable_region_aggregation_args(v)[source]

Check that any variable “region-aggregation” mappings are valid

classmethod check_weight_in_vars(v)[source]

Check that all variables specified in ‘weight’ are present in the codelist

vars_default_args(variables)[source]

return subset of variables which does not feature any special pyam aggregation arguments and where skip_region_aggregation is False

class nomenclature.codelist.RegionCodeList(*, name, mapping={})[source]

A subclass of CodeList specified for regions

Attributes:
namestr

Name of the RegionCodeList

mappingdict

Dictionary of RegionCode objects

Methods

filter(**kwargs)

Filter a CodeList by any attribute-value pairs.

from_directory(name, path[, config, ...])

Initialize a RegionCodeList from a directory with codelist files

filter(**kwargs)

Filter a CodeList by any attribute-value pairs.

Parameters:
**kwargs

Attribute-value mappings to be used for filtering.

Returns:
CodeList

CodeList with Codes that match attribute-value pairs.

classmethod from_directory(name, path, config=None, file_glob_pattern='**/*')[source]

Initialize a RegionCodeList from a directory with codelist files

Parameters:
namestr

Name of the CodeList

pathpathlib.Path or path-like

Directory with the codelist files

configRegionCodeListConfig, optional

Attributes for configuring the CodeList

file_glob_patternstr, optional

Pattern to downselect codelist files by name, default: “**/*” (i.e. all files in all sub-folders)

Returns:
RegionCodeList
property hierarchy

Return the hierarchies defined in the RegionCodeList

Returns:
List[str]