Skip to content

Commit 0038345

Browse files
committed
Pass tests
1 parent b43591c commit 0038345

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/pandas_openscm/accessors/index.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from __future__ import annotations
66

7-
from typing import TYPE_CHECKING, Any, Generic, TypeVar
7+
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar
88

99
import pandas as pd
1010

11-
from pandas_openscm.index_manipulation import ensure_is_multiindex
11+
from pandas_openscm.index_manipulation import ensure_is_multiindex, update_levels
1212

1313
if TYPE_CHECKING:
1414
# Hmm this is somehow not correct.
@@ -72,3 +72,73 @@ def eim(self) -> pd.MultiIndex:
7272
this is a no-op (although the value of copy is respected).
7373
"""
7474
return self.ensure_is_multiindex()
75+
76+
def update_index_levels(
77+
self,
78+
updates: dict[Any, Callable[[Any], Any]],
79+
remove_unused_levels: bool = True,
80+
) -> pd.MultiIndex:
81+
"""
82+
Update the levels
83+
84+
Parameters
85+
----------
86+
updates
87+
Updates to apply
88+
89+
Each key is the level to which the updates will be applied.
90+
Each value is a function which updates the level to its new values.
91+
92+
remove_unused_levels
93+
Remove unused levels before applying the update
94+
95+
Specifically, call
96+
[pd.MultiIndex.remove_unused_levels][pandas.MultiIndex.remove_unused_levels].
97+
98+
This avoids trying to update levels that aren't being used.
99+
100+
Returns
101+
-------
102+
:
103+
[pd.MultiIndex][pandas.MultiIndex] with updates applied
104+
"""
105+
return update_levels(
106+
self._index,
107+
updates=updates,
108+
remove_unused_levels=remove_unused_levels,
109+
)
110+
111+
def update_levels_from_other(
112+
self,
113+
updates: dict[Any, Callable[[Any], Any]],
114+
remove_unused_levels: bool = True,
115+
) -> pd.MultiIndex:
116+
"""
117+
Update the levels
118+
119+
Parameters
120+
----------
121+
updates
122+
Updates to apply
123+
124+
Each key is the level to which the updates will be applied.
125+
Each value is a function which updates the level to its new values.
126+
127+
remove_unused_levels
128+
Remove unused levels before applying the update
129+
130+
Specifically, call
131+
[pd.MultiIndex.remove_unused_levels][pandas.MultiIndex.remove_unused_levels].
132+
133+
This avoids trying to update levels that aren't being used.
134+
135+
Returns
136+
-------
137+
:
138+
[pd.MultiIndex][pandas.MultiIndex] with updates applied
139+
"""
140+
return update_levels(
141+
self._index,
142+
updates=updates,
143+
remove_unused_levels=remove_unused_levels,
144+
)

src/pandas_openscm/indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def multi_index_lookup(pandas_obj: P, locator: pd.MultiIndex) -> P:
176176
if not isinstance(pandas_obj.index, pd.MultiIndex):
177177
msg = (
178178
"This function is only intended to be used "
179-
"when `pobj`'s index is an instance of `MultiIndex`. "
179+
"when `pandas_obj`'s index is an instance of `MultiIndex`. "
180180
f"Received {type(pandas_obj.index)=}"
181181
)
182182
raise TypeError(msg)

0 commit comments

Comments
 (0)