Automated unit handling in operations
See original GitHub issueThis issue shall describe the problem already noted in the PR #535
The _op_data-function doesn’t handle units automatically. I guess, the corresponding functions add, subtract, multiply, divide and apply only work if the timeseries have the same units, but I didn’t test yet what it does if timeseries have different units.
When doing df.multiply('Emission Factor|Gases', 'Final Energy|Gases', 'Emissions|Gases') I’d expect to have Emissions|Gases the units Mt CO2.
To make _op_data handle units automatically pint might be useful. Pint can do calculations with units, even with numpy-arrays. My first thoughts for it:
import iam_units
# load iam-Units
registry = iam_units.registry
# calculate something with pint - this should be adapted to pyam timeseries and the units stored there
a = registry.Quantity(5, 'TW')
b = registry.Quantity(3, 'Mt')
c = a / b
# store the unit as string 'TW / Mt' - this needs to be passed to the pyam timeseries
unit = '{:~}'.format(c.units)
I think it’s important to handle units when calculating on the variables-axis, e.g. emissions = emission factor * energy. However on other axis like regions or scenarios I’m not sure how much it makes sense and how difficult the implementation would be to handle different units at once.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)

Top Related StackOverflow Question
My default here would be to raise an error about the unknown unit or let pint simply raise a dimensionality error or similar… After that, option 2 sounds good with one tweak. If
ignore_unitsisTrue, I would put ‘unknown’ as the unit (empty string in pyam is treated as nan often so might be dangerous…).closed via #541