gcages.assertions#
Useful assertions
Classes:
| Name | Description |
|---|---|
DataIsNotAllNumericError |
Raised when not all data in a pd.DataFrame is numeric |
IndexIsNotMultiIndexError |
Raised when the index is not a pd.MultiIndex |
MissingDataForTimesError |
Raised when a pd.DataFrame is missing data for expected times |
MissingIndexLevelsError |
Raised when a pd.DataFrame is missing expected index levels |
NotAllowedMetadataValuesError |
Raised when a pd.DataFrame contains disallowed metadata values |
Functions:
| Name | Description |
|---|---|
assert_data_is_all_numeric |
Assert that all data in a pd.DataFrame is numeric |
assert_has_data_for_times |
Assert that a pd.DataFrame has data for the given times |
assert_has_index_levels |
Assert that a pd.DataFrame has all the given levels in its index |
assert_index_is_multiindex |
Assert that the index is a pd.MultiIndex |
assert_metadata_values_all_allowed |
Assert that a pd.DataFrame only contains allowed metadata values |
assert_only_working_on_variable_unit_region_variations |
Assert that we're only working on variations in variable, unit and region |
assert_only_working_on_variable_unit_variations |
Assert that we're only working on variations in variable and unit |
DataIsNotAllNumericError #
Bases: ValueError
Raised when not all data in a pd.DataFrame is numeric
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/gcages/assertions.py
__init__ #
__init__(
df: DataFrame, non_numeric_cols: Collection[Any]
) -> None
Initialise the error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame containing non-numeric data |
required |
non_numeric_cols
|
Collection[Any]
|
The columns that contain non-numeric data |
required |
Source code in src/gcages/assertions.py
IndexIsNotMultiIndexError #
Bases: TypeError
Raised when the index is not a pd.MultiIndex
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/gcages/assertions.py
MissingDataForTimesError #
Bases: KeyError
Raised when a pd.DataFrame is missing data for expected times
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/gcages/assertions.py
__init__ #
__init__(
df: DataFrame,
name: str,
missing_times: Collection[Any],
allow_nan: bool,
) -> None
Initialise the error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame that is missing expected index levels |
required |
name
|
str
|
Name of |
required |
missing_times
|
Collection[Any]
|
Times in |
required |
allow_nan
|
bool
|
Were NaN values allowed in the values of |
required |
Source code in src/gcages/assertions.py
MissingIndexLevelsError #
Bases: KeyError
Raised when a pd.DataFrame is missing expected index levels
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/gcages/assertions.py
__init__ #
__init__(
df: DataFrame, missing_levels: Collection[Any]
) -> None
Initialise the error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame that is missing expected index levels |
required |
missing_levels
|
Collection[Any]
|
Levels that are missing from |
required |
Source code in src/gcages/assertions.py
NotAllowedMetadataValuesError #
Bases: ValueError
Raised when a pd.DataFrame contains disallowed metadata values
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/gcages/assertions.py
__init__ #
__init__(
df: DataFrame,
metadata_key: Any,
disallowed_values: Collection[Any],
allowed_values: Collection[Any],
) -> None
Initialise the error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame that contains diasallowed metadata values |
required |
metadata_key
|
Any
|
The metadata key which is being considered (e.g. "variable", "unit") |
required |
disallowed_values
|
Collection[Any]
|
The values which are not allowed but appear in |
required |
allowed_values
|
Collection[Any]
|
The values which are allowed for |
required |
Source code in src/gcages/assertions.py
assert_data_is_all_numeric #
assert_data_is_all_numeric(df: DataFrame) -> None
Assert that all data in a pd.DataFrame is numeric
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame to check |
required |
Raises:
| Type | Description |
|---|---|
DataIsNotAllNumericError
|
If there are columns in |
Source code in src/gcages/assertions.py
assert_has_data_for_times #
assert_has_data_for_times(
df: DataFrame,
name: str,
times: Iterable[Any],
allow_nan: bool,
) -> None
Assert that a pd.DataFrame has data for the given times
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame to check |
required |
name
|
str
|
Name of |
required |
times
|
Iterable[Any]
|
Times (i.e. columns) that we expect to have data in |
required |
allow_nan
|
bool
|
Are NaN values allowed in the values of |
required |
Raises:
| Type | Description |
|---|---|
MissingDataForTimesError
|
The data in If |
Source code in src/gcages/assertions.py
assert_has_index_levels #
Assert that a pd.DataFrame has all the given levels in its index
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame to check |
required |
levels
|
Iterable[Any]
|
Levels that we expect to be in the index of |
required |
Raises:
| Type | Description |
|---|---|
MissingIndexLevelsError
|
The index of |
Source code in src/gcages/assertions.py
assert_index_is_multiindex #
assert_index_is_multiindex(df: DataFrame) -> None
Assert that the index is a pd.MultiIndex
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame to check |
required |
Raises:
| Type | Description |
|---|---|
IndexIsNotMultiIndexError
|
The index of |
Source code in src/gcages/assertions.py
assert_metadata_values_all_allowed #
assert_metadata_values_all_allowed(
df: DataFrame,
metadata_key: Any,
allowed_values: Collection[Any],
) -> None
Assert that a pd.DataFrame only contains allowed metadata values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame to check |
required |
metadata_key
|
Any
|
The metadata key to check (e.g. "variable", "unit") |
required |
allowed_values
|
Collection[Any]
|
The values which are allowed for this metadata key |
required |
Raises:
| Type | Description |
|---|---|
NotAllowedMetadataValuesError
|
There is metadata for |
Source code in src/gcages/assertions.py
assert_only_working_on_variable_unit_region_variations #
assert_only_working_on_variable_unit_region_variations(
indf: DataFrame,
) -> None
Assert that we're only working on variations in variable, unit and region
In other words, we don't have variations in scenarios, models etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indf
|
DataFrame
|
Data to verify |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
There are variations in columns other than variable and unit |
Source code in src/gcages/assertions.py
assert_only_working_on_variable_unit_variations #
assert_only_working_on_variable_unit_variations(
indf: DataFrame,
) -> None
Assert that we're only working on variations in variable and unit
In other words, we don't have variations in scenarios, models etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indf
|
DataFrame
|
Data to verify |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
There are variations in columns other than variable and unit |