Skip to content

Commit be9fccb

Browse files
committed
doctests
1 parent 2b5e35d commit be9fccb

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/pandas_openscm/index_manipulation.py

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def update_levels_from_other(
703703
level_to_create_from=source,
704704
mapper=updater,
705705
)
706-
# TODO copy paste code
706+
707707
if level in ini.names:
708708
level_idx = ini.names.index(level)
709709
levels[level_idx] = new_level
@@ -725,10 +725,18 @@ def create_level_from_collection(
725725
"""
726726
Create new level and corresponding codes.
727727
728-
From a level name and a
729-
collection of values.
728+
Parameters
729+
----------
730+
level
731+
Name of the level to create
730732
731-
TODO
733+
value
734+
Values to use to create the level
735+
736+
Returns
737+
-------
738+
:
739+
New level and corresponding codes
732740
"""
733741
new_level = pd.Index(value, name=level)
734742
if not new_level.has_duplicates:
@@ -768,6 +776,49 @@ def set_levels(
768776
If `ini` is not a MultiIndex
769777
ValueError
770778
If the length of the values is not equal to the length of the index
779+
780+
Examples
781+
--------
782+
>>> start = pd.MultiIndex.from_tuples(
783+
... [
784+
... ("sa", "ma", "v1", "kg"),
785+
... ("sb", "ma", "v2", "m"),
786+
... ("sa", "mb", "v1", "kg"),
787+
... ("sa", "mb", "v2", "m"),
788+
... ],
789+
... names=["scenario", "model", "variable", "unit"],
790+
... )
791+
>>> start
792+
MultiIndex([('sa', 'ma', 'v1', 'kg'),
793+
('sb', 'ma', 'v2', 'm'),
794+
('sa', 'mb', 'v1', 'kg'),
795+
('sa', 'mb', 'v2', 'm')],
796+
names=['scenario', 'model', 'variable', 'unit'])
797+
>>>
798+
>>> # Set a new level with a single value
799+
>>> set_levels(
800+
... start,
801+
... {"new_variable": "xyz"},
802+
... )
803+
MultiIndex([('sa', 'ma', 'v1', 'kg', 'xyz'),
804+
('sb', 'ma', 'v2', 'm', 'xyz'),
805+
('sa', 'mb', 'v1', 'kg', 'xyz'),
806+
('sa', 'mb', 'v2', 'm', 'xyz')],
807+
names=['scenario', 'model', 'variable', 'unit', 'new_variable'])
808+
>>>
809+
>>> # Replace a level with a collection
810+
>>> set_levels(
811+
... start,
812+
... {"new_variable": [1, 2, 3, 4]},
813+
... )
814+
todo
815+
>>>
816+
>>> # Replace a level with a single value and add a new level
817+
>>> set_levels(
818+
... start,
819+
... {"model": "new_model", "new_variable": ["xyz", "xyz", "x", "y"]},
820+
... )
821+
todo
771822
"""
772823
levels: list[pd.Index[Any]] = list(ini.levels)
773824
codes: list[list[int] | npt.NDArray[np.integer[Any]]] = list(ini.codes)

0 commit comments

Comments
 (0)