Skip to content

get_scenario() and __getitem__ fail to reset MultiIndex for empty components #1402

@Irieo

Description

@Irieo

Version Checks (indicate both or one)

  • I have confirmed this bug exists on the lastest release of PyPSA.

  • I have confirmed this bug exists on the current master branch of PyPSA.

Issue Description

get_scenario() leaves MultiIndex on empty components, which breaks n.optimize() call.

The problem is specifically here:

for c in n.components.values():
if not c.static.empty:
c.static = c.static.xs(scenario, level="scenario", axis=0)

n['low'] yields the same problem, so __getitem__ has the same issue.

I will patch both via PR in a moment.

Reproducible Example

import pypsa

n = pypsa.Network()
n.set_snapshots(range(3))
n.add("Bus", "bus")
n.add("Load", "load", bus="bus", p_set=100)
n.add("Generator", "gen", bus="bus", p_nom_extendable=True, capital_cost=1000)

n.set_scenarios({"low": 0.5, "high": 0.5})

n_low = n.get_scenario("low")  # or n["low"]
n_low.optimize(solver_name="highs")  # ValueError: conflicting multi-index level name 'name'

print(n_low.has_scenarios) # False
print(n_low.c.links.static.index) # MultiIndex([], names=['scenario', 'name'])

Expected Behavior

Methods should reset MultiIndex and n.optimize() call should work on retrieved networks.

Installed Versions

v1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions