pandas_openscm.unit_conversion#
Support for unit conversion
Classes:
| Name | Description |
|---|---|
AmbiguousTargetUnitError |
Raised when |
MissingDesiredUnitError |
Raised when the desired unit is not specified for all timeseries |
Functions:
| Name | Description |
|---|---|
convert_unit |
Convert a supported pandas object's units |
convert_unit_from_target_series |
Convert |
convert_unit_like |
Convert units to match another pd.DataFrame |
AmbiguousTargetUnitError #
Bases: ValueError
Raised when target provided to convert_unit_like gives ambiguous desired units
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/pandas_openscm/unit_conversion.py
MissingDesiredUnitError #
Bases: ValueError
Raised when the desired unit is not specified for all timeseries
Methods:
| Name | Description |
|---|---|
__init__ |
Initialise the error |
Source code in src/pandas_openscm/unit_conversion.py
__init__ #
__init__(missing_ts: MultiIndex) -> None
Initialise the error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
missing_ts
|
MultiIndex
|
Timeseries for which no desired unit is specified |
required |
Source code in src/pandas_openscm/unit_conversion.py
convert_unit #
convert_unit(
pobj: P,
desired_units: str | Mapping[str, str] | Series[str],
unit_level: str = "unit",
ur: UnitRegistry | None = None,
) -> P
Convert a supported pandas object's units
This uses convert_unit_from_target_series. If you want to understand the details of how the conversion works, see that function's docstring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pobj
|
P
|
Supported pandas object whose units should be converted |
required |
desired_units
|
str | Mapping[str, str] | Series[str]
|
Desired unit(s) for If this is a string,
we attempt to convert all timeseries in If this is a mapping,
we convert the given units to the target units.
Be careful using this form - you need to be certain of the units in If this is a pd.Series,
then it will be passed to convert_unit_from_target_series
after filling any rows in For further details, see examples |
required |
unit_level
|
str
|
Level in Passed to convert_unit_from_target_series. |
'unit'
|
ur
|
UnitRegistry | None
|
Unit registry to use for the conversion. Passed to convert_unit_from_target_series. |
None
|
Returns:
| Type | Description |
|---|---|
P
|
|
Examples:
>>> import pandas as pd
>>>
>>> start = pd.DataFrame(
... [[1.0, 2.0, 3.0], [1.1, 1.2, 1.3], [37.0, 38.1, 37.9]],
... columns=[2020, 2030, 2050],
... index=pd.MultiIndex.from_tuples(
... (
... ("sa", "temperature", "mK"),
... ("sb", "temperature", "K"),
... ("sb", "body temperature", "degC"),
... ),
... names=["scenario", "variable", "unit"],
... ),
... )
>>>
>>> # Convert all timeseries to a given unit
>>> convert_unit(start, "K")
2020 2030 2050
scenario variable unit
sa temperature K 0.001 0.002 0.003
sb temperature K 1.100 1.200 1.300
body temperature K 310.150 311.250 311.050
>>>
>>> # Same thing with a series as input
>>> convert_unit(start[2030], "K")
scenario variable unit
sa temperature K 0.002
sb temperature K 1.200
body temperature K 311.250
Name: 2030, dtype: float64
>>>
>>> # Convert using a mapping.
>>> # Units that aren't specified in the mapping aren't converted.
>>> convert_unit(start, {"mK": "K", "K": "kK"})
2020 2030 2050
scenario variable unit
sa temperature K 0.0010 0.0020 0.0030
sb temperature kK 0.0011 0.0012 0.0013
body temperature degC 37.0000 38.1000 37.9000
>>>
>>> # When using a mapping, be careful.
>>> # If you have a typo, there will be no conversion but also no error.
>>> convert_unit(start, {"MK": "K", "K": "kK"})
2020 2030 2050
scenario variable unit
sa temperature mK 1.0000 2.0000 3.0000
sb temperature kK 0.0011 0.0012 0.0013
body temperature degC 37.0000 38.1000 37.9000
>>>
>>> # Convert using a series
>>> convert_unit(
... start,
... pd.Series(
... ["K", "degF"],
... index=pd.MultiIndex.from_tuples(
... (
... ("sa", "temperature"),
... # ("sb", "temperature") not included therefore not converted
... ("sb", "body temperature"),
... ),
... names=["scenario", "variable"],
... ),
... ),
... )
2020 2030 2050
scenario variable unit
sa temperature K 0.001 0.002 0.003
sb temperature K 1.100 1.200 1.300
body temperature degF 98.600 100.580 100.220
Source code in src/pandas_openscm/unit_conversion.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
convert_unit_from_target_series #
convert_unit_from_target_series(
pobj: P,
desired_units: Series[str],
unit_level: str = "unit",
ur: UnitRegistry | None = None,
) -> P
Convert pobj's units based on a pd.Series
desired_uni defines the units to convert to.
This is a relatively low-level function,
you may find convert_unit and convert_unit_like easier to use.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pobj
|
P
|
Supported pandas object whose units should be converted |
required |
desired_units
|
Series[str]
|
Desired unit(s) for This must be a pd.Series
with an index that contains all the rows in |
required |
unit_level
|
str
|
Level in |
'unit'
|
ur
|
UnitRegistry | None
|
Unit registry to use for the conversion. If not supplied, we use pint.get_application_registry. |
None
|
Returns:
| Type | Description |
|---|---|
P
|
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
|
MissingOptionalDependencyError
|
|
Examples:
>>> import pandas as pd
>>>
>>> start = pd.DataFrame(
... [[1.0, 2.0, 3.0], [1.1, 1.2, 1.3], [37.0, 38.1, 37.9]],
... columns=[2020, 2030, 2050],
... index=pd.MultiIndex.from_tuples(
... (
... ("sa", "temperature", "mK"),
... ("sb", "temperature", "K"),
... ("sb", "body temperature", "degC"),
... ),
... names=["scenario", "variable", "unit"],
... ),
... )
>>>
>>> convert_unit_from_target_series(
... start,
... desired_units=pd.Series(
... ["K", "mK", "degF"],
... index=pd.MultiIndex.from_tuples(
... (
... ("sa", "temperature"),
... ("sb", "temperature"),
... ("sb", "body temperature"),
... ),
... names=["scenario", "variable"],
... ),
... ),
... )
2020 2030 2050
scenario variable unit
sa temperature K 0.001 0.002 0.003
sb temperature mK 1100.000 1200.000 1300.000
body temperature degF 98.600 100.580 100.220
Source code in src/pandas_openscm/unit_conversion.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 | |
convert_unit_like #
convert_unit_like(
pobj: P,
target: DataFrame | Series[Any],
unit_level: str = "unit",
target_unit_level: str | None = None,
ur: UnitRegistry | None = None,
) -> P
Convert units to match another pd.DataFrame
This is essentially a helper function for convert_unit_from_target_series.
It implements one set of logic for extracting desired units and tries to be clever,
handling differences in index levels
between pobj and target sensibly wherever possible.
If you want behaviour other than what is implemented here, use convert_unit_from_target_series directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pobj
|
P
|
Supported pandas object whose units should be converted |
required |
target
|
DataFrame | Series[Any]
|
Supported pandas object whose units should be matched |
required |
unit_level
|
str
|
Level in |
'unit'
|
target_unit_level
|
str | None
|
Level in If not supplied, we use |
None
|
ur
|
UnitRegistry | None
|
Unit registry to use for the conversion. Passed to convert_unit_from_target_series. |
None
|
Returns:
| Type | Description |
|---|---|
P
|
|
Examples:
>>> import pandas as pd
>>>
>>> start = pd.DataFrame(
... [
... [1010.0, 2010.0, 1150.0],
... [100.1, 100.3, 99.8],
... [0.0011, 0.0012, 0.0013],
... [310_000, 311_000, 310_298],
... ],
... columns=[2020, 2030, 2050],
... index=pd.MultiIndex.from_tuples(
... (
... ("sa", "temperature", "mK"),
... ("sa", "body temperature", "degF"),
... ("sb", "temperature", "kK"),
... ("sb", "body temperature", "mK"),
... ),
... names=["scenario", "variable", "unit"],
... ),
... )
>>>
>>> target = pd.DataFrame(
... [[1.0, 2.0], [1.1, 1.2]],
... columns=[1990.0, 2010.0],
... index=pd.MultiIndex.from_tuples(
... (
... ("temperature", "K"),
... ("body temperature", "degC"),
... ),
... names=["variable", "unit"],
... ),
... )
>>>
>>> convert_unit_like(start, target)
2020 2030 2050
scenario variable unit
sa temperature K 1.010000 2.010000 1.150000
body temperature degC 37.833333 37.944444 37.666667
sb temperature K 1.100000 1.200000 1.300000
body temperature degC 36.850000 37.850000 37.148000
Source code in src/pandas_openscm/unit_conversion.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |