Skip to content

Commit 4c079b7

Browse files
committed
WIP: convert with dict
1 parent 8b351ac commit 4c079b7

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/pandas_openscm/unit_conversion.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ def convert_unit(
4242
unit_map["target_unit"] = desired_unit
4343

4444
elif isinstance(desired_unit, Mapping):
45-
pass
45+
# TODO: add check for any keys in desired_unit which are not units in df.
46+
# Optionally raise if there are extra keys to avoid silent failure.
47+
target_units_s = df_units_s.map(desired_unit).dropna().rename("target_unit")
48+
unit_map = pd.DataFrame(
49+
[df_units_s.loc[target_units_s.index], target_units_s]
50+
).T
51+
52+
elif isinstance(desired_unit, pd.Series):
53+
raise NotImplementedError
54+
# Assume that desired_unit is already the target units
55+
unit_map = pd.DataFrame([*desired_unit.align(df_units_s)]).T
4656

4757
else:
4858
raise NotImplementedError(type(desired_unit))

tests/integration/test_unit_conversion.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,39 @@ def test_convert_unit_ur_injection():
7474

7575
def test_convert_unit_mapping():
7676
start = create_test_df(
77-
variables=[(f"variable_{i}", "Mt") for i in range(5)],
78-
n_scenarios=3,
79-
n_runs=6,
80-
timepoints=np.array([1.0, 2.0, 3.0]),
77+
variables=[
78+
("co2_emissions", "Mt CO2/yr"),
79+
("erf", "W / m^2"),
80+
("ohc", "ZJ"),
81+
],
82+
n_scenarios=2,
83+
n_runs=2,
84+
timepoints=np.array([1850.0, 2000.0, 2050.0, 2100.0]),
8185
)
8286

8387
# Don't convert W / m^2
84-
res = start.convert_unit({"Mt CO2/yr": "Gt C/yr", "ZJ": "J"})
88+
res = convert_unit(start, {"Mt CO2/yr": "Gt C/yr", "ZJ": "J"})
8589

8690
np.testing.assert_equal(
87-
res.iloc[0, :].values,
88-
start.iloc[0, :].values * 12.0 / 44000.0,
91+
res.loc[res.index.get_level_values("variable") == "co2_emissions", :].values,
92+
12.0
93+
/ 44_000.0
94+
* start.loc[
95+
start.index.get_level_values("variable") == "co2_emissions", :
96+
].values,
8997
)
9098

9199
np.testing.assert_equal(
92-
res.iloc[1, :].values,
93-
start.iloc[1, :].values,
100+
res.loc[res.index.get_level_values("variable") == "erf", :].values,
101+
start.loc[start.index.get_level_values("variable") == "erf", :].values,
94102
)
95103

96104
np.testing.assert_equal(
97-
res.iloc[2, :].values,
98-
start.iloc[2, :].values * 1e21,
105+
res.loc[res.index.get_level_values("variable") == "ohc", :].values,
106+
1e21 * start.loc[start.index.get_level_values("variable") == "ohc", :].values,
99107
)
100108

101109

102-
# - test error paths
103110
def test_convert_unit_like():
104111
start = create_test_df(
105112
variables=[(f"variable_{i}", "Mt") for i in range(5)],
@@ -223,3 +230,8 @@ def test_convert_unit_like_different_unit_level_explicit_target_level():
223230
res.iloc[2, :].values,
224231
start.iloc[2, :].values + 273.0,
225232
)
233+
234+
235+
# To write:
236+
# - no op i.e. what happens when df is already in the right units
237+
# - tests of various error paths

0 commit comments

Comments
 (0)