nudb_use.metadata.nudb_config package

nudb_use.metadata.nudb_config.find_var_missing module

Lookup helpers for NUDB variable metadata defined in config.

explain_rename(var_name, dataset_name=None)

Explain the ranking and selection of historical rename candidates.

Ranks are computed per lookup without changing settings. Their first value is 1 for variable renames, 2 for avslutta, 3 for igang, 4 for eksamen and 5 for other datasets. The second value is the config position within that tier, with a family’s base dataset always at position 0. Child datasets and other datasets follow the loaded config order.

An explicit dataset selects its exact mapping first, then its base family and siblings in rank order, then the overall ranking. Eligibility shows which candidates remain after this contextual selection. Equally ranked conflicting targets have no selected row. Current variable names are used directly unless their dataset family overrides them; these rows have no rank.

Parameters:
  • var_name (str) – Historical name to explain, compared case-insensitively.

  • dataset_name (str | None) – Dataset whose exact and family mappings take precedence.

Returns:

Candidate targets, sources, ranks, eligibility and selection.

Return type:

pd.DataFrame

find_var(var_name, dataset_name=None)

Retrieve configuration and KLASS metadata for a single variable.

Without a dataset, general variable renames rank first, then avslutta, igang, eksamen and other datasets. Base datasets precede their children; children and other datasets follow their order in the loaded config. With a dataset, try its exact override, then its base family and siblings, then the overall ranking. Reordering config datasets can change the result. Conflicting general renames raise ValueError. Use explain_rename to inspect candidate sources, computed ranks and the selected mapping.

Parameters:
  • var_name (str) – Variable name (current or historical). Comparison is case-insensitive.

  • dataset_name (str | None) – Dataset whose exact and family mappings take precedence over the overall ranking, separately for each historical name.

Return type:

VariableMetadata | None

find_var_renames(var_names, dataset_name=None)

Find only the renames of variables from the config, filtering away other info.

Selection follows find_var’s dataset-family and config-order ranking.

Parameters:
  • var_names (list[str]) – List of string-names of columns, can be the old or new names…

  • dataset_name (str | None) – Dataset used to disambiguate historical names.

Returns:

Key is new variable name, values are the old variable names as a list of strings.

Return type:

dict[str, list[str]]

find_var_renames_for_dataset(dataset_name)

Find the renames for a given dataset, from the config.

Parameters:

dataset_name (str) – The name of the dataset according to the config.

Returns:

Key is new variable name, values are the old variable names as a list of strings.

Return type:

dict[str, list[str]]

find_vars(var_names, dataset_name=None)

Look up multiple variables and return their configuration metadata.

Selection follows find_var’s dataset-family and config-order ranking. Use explain_rename to inspect a historical name’s candidate ranks.

Parameters:
  • var_names (Iterable[str]) – Iterable of variable identifiers to resolve.

  • dataset_name (str | None) – Dataset used to disambiguate historical names.

Returns:

Mapping of requested names to their resolved metadata. Missing entries map to None.

Return type:

dict[str, VariableMetadata | None]

get_list_of_columns_for_dataset(dataset_name)

Get a list of str for the columns we expect to be in a given dataset according to the config.

Parameters:

dataset_name (str) – The name of the dataset according to the config.

Returns:

A list of all the column names.

Return type:

list[str]

Raises:

KeyError – If the dataset_name does not exist under datasets in the config settings.

look_up_dtype_length_for_dataset(dataset_name, display_markdown=True)

Make a str of the dtypes and length fields in a dataset from a dataset_name.

Parameters:
  • dataset_name (str) – The name of the dataset according to the config.

  • display_markdown (bool) – If we should display produced str as markdown (works best in notebooks).

Returns:

A str with line shifts per variable. If display_markdown is True, we return None.

Return type:

str | None

variables_missing_from_config(col_list)

Identifies variables that are not defined in the settings.

Parameters:

col_list (Iterable[str]) – An iterable of variable names to check against the settings.

Returns:

Variable names that are not defined in the settings. Returns an empty list if all variables are found.

Return type:

list[str]

nudb_use.metadata.nudb_config.get_dataset_info module

datasetname_teams_buckets(teams_include=None, exclude_nudbs_indata=True)

Get the dataset names, teams and buckets you should get permissions for, that nudb uses.

Parameters:
  • teams_include (list[str] | None) – Team names that we should include from, if left as None, all teams are included.

  • exclude_nudbs_indata (bool) – Exclude data from the education teams that deliver data to NUDB, set to False if you want to include them.

Returns:

Keys are dataset name, team and bucketname in the nested dict.

Return type:

dict[str, dict[str, str]]

unique_teams_buckets(teams_include=None, exclude_nudbs_indata=True)

Get the teams and buckets you should get permissions for, that nudb uses.

Parameters:
  • teams_include (list[str] | None) – Team names that we should include from, if left as None, all teams are included.

  • exclude_nudbs_indata (bool) – Exclude data from the education teams that deliver data to NUDB, set to False if you want to include them.

Returns:

A set of unique teams and buckets that ssb-nudb-config is set up to use.

Return type:

set[tuple[str, str]]

nudb_use.metadata.nudb_config.get_variable_info module

Helpers for reading NUDB variable metadata from configuration.

get_toml_field(toml, field)

Return a field from a TOML object or None if it is missing.

Parameters:
  • toml (Mapping[str, Any]) – Parsed TOML object.

  • field (str) – Field name to retrieve.

Returns:

Field value when present, otherwise None.

Return type:

object | None

get_var_metadata(variables=None)

Get at pandas dataframe of the variable-data from the config.

Parameters:

variables (list[str] | None) – Variables to return data on, if None returns all variables.

Returns:

The information from the metadata.

Return type:

pd.DataFrame

nudb_use.metadata.nudb_config.map_get_dtypes module

Utilities for mapping NUDB variable types to concrete dtype strings.

cast_to_preferred_dtypes(df)

Cast dataframe dtypes to preferred modern types that handle nullable cells better.

  • int64 -> Int64 (nullable integer)

  • float64 -> Float64 (nullable float)

  • bool -> boolean[pyarrow] (nullable boolean)

  • str, object, category, string -> string[pyarrow] (nullable string)

  • datetime64[ns] -> datetime64[s] (second precision)

Parameters:

df (DataFrame) – The dataframe to set dtypes for.

Returns:

The modified pandas dataframe.

Return type:

pd.DataFrame

get_dtype_from_dict(dtype, mapping, datetimes_as_string=False)

Resolve a dtype string through a mapping with optional datetime override.

Parameters:
  • dtype (str) – Logical dtype name from configuration.

  • mapping (dict[str, str] | dict[Literal['STRING', 'DATETIME', 'INTEGER', 'FLOAT', 'BOOLEAN', 'UINTEGER'], Literal['datetime64[s]', 'string[pyarrow]', 'Int64', 'Float64', 'bool[pyarrow]', 'UInt64']]) – Mapping of logical names to engine-specific dtype strings.

  • datetimes_as_string (bool) – When True, map datetime fields to string equivalents.

Returns:

Engine-specific dtype string.

Return type:

str

Raises:

ValueError – If dtype is not defined in mapping.

get_dtypes(vars_map, engine='pandas', datetimes_as_string=False, dataset_name=None)

Build a dtype mapping for a set of variables based on config metadata.

Multiple rename targets are accepted when their configured dtypes agree. Conflicting dtypes raise ValueError with the targets and mapping sources. Ranking does not hide dtype conflicts when no dataset is supplied. An explicit dataset tries its exact mapping, then its base family and siblings in config order, then general variable renames. If none applies, all other dataset mappings must agree on dtype. Base family mappings precede children.

Parameters:
  • vars_map (list[str]) – Variable names to map, including historical names.

  • engine (str) – Mapping preset to use.

  • datetimes_as_string (bool) – When True, convert datetime variables to string dtypes.

  • dataset_name (str | None) – Dataset used to select rename overrides, or None for all mappings.

Returns:

Mapping of requested variables to dtype strings. Variables not found in config map to None.

Return type:

dict[str, str | None]

map_dtype_datadoc(dtype, engine='pandas', datetimes_as_string=False)

Map a logical dtype using a named engine mapping.

Parameters:
  • dtype (str) – Logical dtype value from the NUDB config.

  • engine (str) – Name of the mapping preset to use.

  • datetimes_as_string (bool) – When True, map datetimes to string dtypes.

Returns:

Concrete dtype string for the target engine.

Return type:

str

Raises:

KeyError – If engine is not defined in DTYPE_MAPPINGS.

map_to_preferred_dtypes(df)

Get mapping from dataframe dtypes to preferred modern types that handle nullable cells better.

  • int64 -> Int64 (nullable integer)

  • float64 -> Float64 (nullable float)

  • bool -> boolean[pyarrow] (nullable boolean)

  • str, object, category, string -> string[pyarrow] (nullable string)

  • datetime64[ns] -> datetime64[s] (second precision)

Parameters:

df (DataFrame) – The dataframe to set dtypes for.

Returns:

Dict with mapping from column names to new dtypes.

Return type:

dict[str, str]

nudb_use.metadata.nudb_config.set_options module

set_option(setting_name, value)

Set an option in the options part of the nudb_config package.

Parameters:
  • setting_name (str) – The name of the setting to set.

  • value (Any) – The value we should set the setting to.

Returns:

The changed config settings-object.

Return type:

NudbConfig

nudb_use.metadata.nudb_config.variable_names module

Tools for sorting and renaming NUDB variables based on config metadata.

get_cols2drop(data, name=None)

Return column names to drop from a dataset based on settings_use.

Parameters:
  • data (DataFrame) – DataFrame to check.

  • name (str | None) – Name of the dataset to compare against settings_use.

Returns:

Columns present in the DataFrame but not defined in settings_use.

Return type:

pd.Index

get_cols2keep(data, name=None)

Get column names to keep in a dataset based on settings_use.

Parameters:
  • data (DataFrame) – DataFrame to check.

  • name (str | None) – Name of the dataset to compare against settings_use.

Returns:

Columns present in the dataset that are defined in settings_use.

Return type:

pd.Index

get_cols_in_config(name)

Retrieve column (variable) names from settings_use.

Parameters:

name (str | None) – Name of a dataset. If None, returns all variable names across datasets.

Returns:

List of column or variable names defined in settings_use.

Return type:

list[str]

Raises:

KeyError – If the provided dataset name is not defined in settings_use.

handle_dataset_specific_renames(df, dataset_name='')

Apply dataset-specific rename overrides defined in configuration.

Parameters:
  • df (DataFrame) – DataFrame whose columns should be updated in place.

  • dataset_name (str) – Dataset key. Try its exact override, then its base family and siblings in config order, then the overall ranking: general variable renames, avslutta, igang, eksamen and other datasets.

Returns:

DataFrame with overrides applied if they exist in the config.

Return type:

pd.DataFrame

sort_cols_after_config_order(data)

Sort DataFrame columns according to the config-defined order.

Parameters:

data (DataFrame) – DataFrame whose columns should be reordered.

Returns:

DataFrame with columns reordered per config definition.

Return type:

pd.DataFrame

sort_cols_after_config_order_and_unit(data)

Sort columns after order defined in the config and by unit.

Nested function that combined the functionality of the sort_cols_by_unit and sort_cols_after_config_order functions.

Parameters:

data (DataFrame) – Dataframe to sort.

Returns:

Dataframe with columns reordered by the column and unit order defined in the config.

Return type:

pd.DataFrame

sort_cols_by_unit(data)

Sort DataFrame columns based on the unit of each variable.

Parameters:

data (DataFrame) – Input DataFrame with columns representing variables to sort.

Returns:

DataFrame with columns reordered by their units’ sort order.

Return type:

pd.DataFrame

Raises:

ValueError – If no config is found for the sort unit.

update_colnames(data, dataset_name='', lowercase=True)

Rename columns in a DataFrame based on metadata mappings.

Without a dataset, general variable renames rank first, then avslutta, igang, eksamen and other datasets. Base datasets precede their children; children and other datasets follow their order in the loaded config. With a dataset, exact overrides come first, then its base family and siblings in config order, then the overall ranking. Conflicting general renames raise ValueError. Reordering config datasets can change the selected rename.

Parameters:
  • data (DataFrame) – Input DataFrame whose column names should be updated.

  • dataset_name (str) – Dataset identifier for applying dataset-specific overrides.

  • lowercase (bool) – Whether to lowercase column names before renaming.

Returns:

Copy of the DataFrame with columns renamed according to metadata.

Return type:

pd.DataFrame

Raises:

KeyError – If the renaming results in duplicate column names.