gcages.units_helpers#
Helpers for unit handling
Functions:
| Name | Description |
|---|---|
assert_has_no_pint_incompatible_characters |
Assert that a collection does not contain pint-incompatible characters |
convert_unit_like |
Convert to units like a different pd.DataFrame |
strip_pint_incompatible_characters_from_unit_string |
Strip pint-incompatible characters from a unit string |
strip_pint_incompatible_characters_from_units |
Strip pint-incompatible characters from units |
assert_has_no_pint_incompatible_characters #
assert_has_no_pint_incompatible_characters(
units: Collection[str],
pint_incompatible_characters: Collection[str] = {"-"},
) -> None
Assert that a collection does not contain pint-incompatible characters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
units
|
Collection[str]
|
Collection to check This is named |
required |
pint_incompatible_characters
|
Collection[str]
|
Characters which are incompatible with pint You should not need to change this, but it is made an argument just in case |
{'-'}
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
|
Source code in src/gcages/units_helpers.py
convert_unit_like #
convert_unit_like(
df: DataFrame,
target: DataFrame,
df_unit_level: str = "unit",
target_unit_level: str | None = None,
ur: UnitRegistry | None = None,
) -> DataFrame
Convert to units like a different pd.DataFrame
Parameters#
df pd.DataFrame of which to convert the units
target pd.DataFrame whose units the result should match
df_unit_level
Level in df's index which has unit information
target_unit_level
Level in target's index which has unit information
If not provided, we assume this is the same as `df_unit_level`
ur Unit registry to use for determining unit conversions
If not provided, we use [openscm_units.unit_registry][openscm_units.unit_registry]
Returns#
:
df with units that match target
Raises#
MissingOptionalDependencyError
ur is None and openscm-units is not installed
Source code in src/gcages/units_helpers.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
strip_pint_incompatible_characters_from_unit_string #
Strip pint-incompatible characters from a unit string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unit_str
|
str
|
Unit string from which to strip pint-incompatible characters |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in src/gcages/units_helpers.py
strip_pint_incompatible_characters_from_units #
strip_pint_incompatible_characters_from_units(
indf: DataFrame, units_index_level: str = "unit"
) -> DataFrame
Strip pint-incompatible characters from units
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indf
|
DataFrame
|
Input data from which to strip pint-incompatible characters |
required |
units_index_level
|
str
|
Column in |
'unit'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|