Skip to content

Commit a435b33

Browse files
committed
Add index accessor and tests
1 parent 841bf90 commit a435b33

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

src/pandas_openscm/accessors/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def eim(self) -> pd.MultiIndex:
7777
"""
7878
return self.ensure_is_multiindex()
7979

80-
def update_index_levels(
80+
def update_levels(
8181
self,
8282
updates: dict[Any, Callable[[Any], Any]],
8383
remove_unused_levels: bool = True,

tests/integration/index_manipulation/test_integration_index_manipulation_update_levels.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,50 @@ def test_update_levels_missing_level():
132132
update_levels(start, updates=updates)
133133

134134

135+
def test_accessor_index(setup_pandas_accessors):
136+
start = pd.MultiIndex.from_tuples(
137+
[
138+
("sa", "va", "kg", 0),
139+
("sb", "vb", "m", -1),
140+
("sa", "va", "kg", -2),
141+
("sa", "vb", "kg", 2),
142+
],
143+
names=["scenario", "variable", "unit", "run_id"],
144+
)
145+
updates = {
146+
"variable": lambda x: x.replace("v", "vv"),
147+
"unit": lambda x: x.replace("kg", "g").replace("m", "km"),
148+
}
149+
150+
res = start.openscm.update_levels(updates=updates)
151+
152+
exp = pd.MultiIndex.from_tuples(
153+
[
154+
("sa", "vva", "g", 0),
155+
("sb", "vvb", "km", -1),
156+
("sa", "vva", "g", -2),
157+
("sa", "vvb", "g", 2),
158+
],
159+
names=["scenario", "variable", "unit", "run_id"],
160+
)
161+
162+
pd.testing.assert_index_equal(res, exp)
163+
164+
165+
def test_accessor_index_not_multiindex(setup_pandas_accessors):
166+
start = pd.Index([1, 2, 3])
167+
168+
with pytest.raises(
169+
TypeError,
170+
match=re.escape(
171+
"This method is only intended to be used "
172+
"when index is an instance of `MultiIndex`. "
173+
)
174+
+ "Received .*Index.*'",
175+
):
176+
start.openscm.update_levels(updates={})
177+
178+
135179
def test_doesnt_trip_over_droped_levels(setup_pandas_accessors):
136180
def update_func(in_v: int) -> int:
137181
if in_v < 0:

tests/integration/index_manipulation/test_integration_index_manipulation_update_levels_from_other.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,50 @@ def test_update_levels_from_other_missing_levels(sources, exp):
282282
update_levels_from_other(start, update_sources=update_sources)
283283

284284

285+
def test_accessor_index(setup_pandas_accessors):
286+
start = pd.MultiIndex.from_tuples(
287+
[
288+
("sa", "va", "kg", 0),
289+
("sb", "vb", "m", -1),
290+
("sa", "va", "kg", -2),
291+
("sa", "vb", "kg", 2),
292+
],
293+
names=["scenario", "variable", "unit", "run_id"],
294+
)
295+
update_sources = {
296+
"vv": ("variable", lambda x: x.replace("v", "vv")),
297+
"uu": ("unit", lambda x: x.replace("kg", "g").replace("m", "km")),
298+
}
299+
300+
res = start.openscm.update_levels_from_other(update_sources=update_sources)
301+
302+
exp = pd.MultiIndex.from_tuples(
303+
[
304+
("sa", "va", "kg", 0, "vva", "g"),
305+
("sb", "vb", "m", -1, "vvb", "km"),
306+
("sa", "va", "kg", -2, "vva", "g"),
307+
("sa", "vb", "kg", 2, "vvb", "g"),
308+
],
309+
names=["scenario", "variable", "unit", "run_id", "vv", "uu"],
310+
)
311+
312+
pd.testing.assert_index_equal(res, exp)
313+
314+
315+
def test_accessor_index_not_multiindex(setup_pandas_accessors):
316+
start = pd.Index([1, 2, 3])
317+
318+
with pytest.raises(
319+
TypeError,
320+
match=re.escape(
321+
"This method is only intended to be used "
322+
"when index is an instance of `MultiIndex`. "
323+
)
324+
+ "Received .*Index.*'",
325+
):
326+
start.openscm.update_levels_from_other(update_sources={})
327+
328+
285329
def test_doesnt_trip_over_dropped_levels(setup_pandas_accessors):
286330
def update_func(in_v: int) -> int:
287331
if in_v < 0:

0 commit comments

Comments
 (0)