Skip to content

NetworkTransformMixin


              flowchart TD
              pypsa.Network[NetworkTransformMixin]
              pypsa.network.abstract._NetworkABC[_NetworkABC]

                              pypsa.network.abstract._NetworkABC --> pypsa.Network
                


              click pypsa.Network href "" "pypsa.Network"
              click pypsa.network.abstract._NetworkABC href "" "pypsa.network.abstract._NetworkABC"
            

Mixin class for network transform methods.

Class inherits to pypsa.Network. All attributes and methods can be used within any Network instance.

Methods:

  • add

    Add components to the network.

  • merge

    Merge the components of two networks.

  • remove

    Remove a single component or a list of components from the network.

  • rename_component_names

    Rename component names.

pypsa.Network.add

add(class_name: str, name: str | int | Sequence[int | str], suffix: str = '', overwrite: bool = False, return_names: bool | None = None, **kwargs: Any) -> Index | None

Add components to the network.

Handles addition of single and multiple components along with their attributes. Pass a list of names to add multiple components at once or pass a single name to add a single component.

When a single component is added, all non-scalar attributes are assumed to be time-varying and indexed by snapshots. When multiple components are added, all non-scalar attributes are assumed to be static and indexed by names. A single value sequence is treated as scalar and broadcasted to all components. It is recommended to explicitly pass a scalar instead. If you want to add time-varying attributes to multiple components, you can pass a 2D array/ DataFrame where the first dimension is snapshots and the second dimension is names.

Any attributes which are not specified will be given the default value from Components.

Parameters:

  • class_name (str) –

    Component class name in ("Bus", "Generator", "Load", "StorageUnit", "Store", "ShuntImpedance", "Line", "Transformer", "Link", "Process").

  • name (str or int or list of str or list of int) –

    Component name(s)

  • suffix (str, default: "" ) –

    All components are named after name with this added suffix.

  • overwrite (bool, default: False ) –

    If True, existing components with the same names as in name will be overwritten. Otherwise only new components will be added and others will be ignored.

  • return_names (bool | None, default: None ) –

    Whether to return the names of the components added. Defaults to module wide option (default: False). See https://go.pypsa.org/options-params for more information.

  • kwargs (Any, default: {} ) –

    Component attributes, e.g. x=[0.1, 0.2], can be list, pandas.Series or pandas.DataFrame for time-varying

Returns:

  • new_names ( index or None ) –

    Names of new components (including suffix) if return_names is True, otherwise None

Examples:

Add a single component:

>>> n = pypsa.Network()
>>> n.add("Bus", "my_bus_0")
>>> n.add("Bus", "my_bus_1", v_nom=380)
>>> n.add("Line", "my_line_name", bus0="my_bus_0", bus1="my_bus_1", length=34, r=2, x=4)

Add multiple components with static attributes:

>>> n.add("Load", ["load 1", "load 2"],
...       bus=["1", "2"],
...       p_set=np.random.rand(len(n.snapshots), 2))

Add multiple components with time-varying attributes:

>>> import pandas as pd, numpy as np
>>> buses = range(13)
>>> snapshots = range(7)
>>> n = pypsa.Network()
>>> n.set_snapshots(snapshots)
>>> n.add("Bus", buses)
>>> # add load as numpy array
>>> n.add("Load",
...       n.buses.index + " load",
...       bus=buses,
...       p_set=np.random.rand(len(snapshots), len(buses)))
>>> # add wind availability as pandas DataFrame
>>> wind = pd.DataFrame(np.random.rand(len(snapshots), len(buses)),
...        index=n.snapshots,
...        columns=buses)
>>> # use a suffix to avoid boilerplate to rename everything
>>> n.add("Generator",
...       buses,
...       suffix=' wind',
...       bus=buses,
...       p_nom_extendable=True,
...       capital_cost=1e5,
...       p_max_pu=wind)

pypsa.Network.merge

merge(other: Network, components_to_skip: Collection[str] | None = None, inplace: bool = False, with_time: bool = True) -> Any

Merge the components of two networks.

Requires disjunct sets of component indices and, if time-dependent data is merged, identical snapshots and snapshot weightings.

If a component in ther does not have values for attributes present in n, default values are set.

If a component in other has attributes which are not present in n these attributes are ignored.

Parameters:

  • other (Network) –

    Network to add from.

  • components_to_skip (list - like, default: None ) –

    List of names of components which are not to be merged e.g. "Bus"

  • inplace (bool, default: False ) –

    If True, merge into n in-place, otherwise a copy is made.

  • with_time (bool, default: True ) –

    If False, only static data is merged.

Returns:

  • receiving_n ( Network ) –

    Merged network, or None if inplace=True

pypsa.Network.remove

remove(class_name: str, name: str | int | Sequence[int | str], suffix: str = '') -> None

Remove a single component or a list of components from the network.

Removes it from component DataFrames.

Parameters:

  • class_name (str) –

    Component class name

  • name ((str, int, list - like or Index)) –

    Component name(s)

  • suffix (str, default: '' ) –

    Suffix to be added to the component name(s)

Examples:

>>> n = pypsa.Network()
>>> n.snapshots = pd.date_range("2015-01-01", freq="h", periods=2)
>>> n.add("Bus", ["bus0", "bus1"])
>>> n.add("Bus", "bus2", p_min_pu=[1, 1])
>>> n.components.buses.static
       v_nom type    x    y  ... v_mag_pu_max control generator  sub_network
name                             ...
bus0         1.0       0.0  0.0  ...          inf      PQ
bus1         1.0       0.0  0.0  ...          inf      PQ
bus2         1.0       0.0  0.0  ...          inf      PQ

[3 rows x 13 columns]

Remove a single component:

>>> n.remove("Bus", "bus2")

Any component data is dropped from the component DataFrames.

>>> n.components.buses.static
       v_nom type    x    y  ... v_mag_pu_max control generator  sub_network
name                             ...
bus0         1.0       0.0  0.0  ...          inf      PQ
bus1         1.0       0.0  0.0  ...          inf      PQ

[2 rows x 13 columns]
>>> n.components.buses.dynamic.p_min_pu
Empty DataFrame
Columns: []
Index: [2015-01-01 00:00:00, 2015-01-01 01:00:00]

Remove multiple components:

>>> n.remove("Bus", ["bus0", "bus1"])
>>> n.components.buses.static
Empty DataFrame
Columns: [v_nom, type, x, y, carrier, unit, location, v_mag_pu_set, v_mag_pu_min, v_mag_pu_max, control, generator, sub_network]
Index: []

pypsa.Network.rename_component_names

rename_component_names(component: str | Components, **kwargs: str) -> None

Rename component names.

Rename components of component type and also update all cross-references of the component in network.

Parameters:

  • component (str or Components) –

    Component type or instance of pypsa.Components.

  • **kwargs (str, default: {} ) –

    Mapping of old names to new names.

Examples:

Define some network

>>> n = pypsa.Network()
>>> n.add("Bus", ["bus1"])
>>> n.add("Generator", ["gen1"], bus="bus1")

Now rename the bus component

>>> n.rename_component_names("Bus", bus1="bus2")

Which updates the bus components

>>> n.buses.index
Index(['bus2'], dtype='object', name='name')

and all references in the network

>>> n.generators.bus
name
gen1    bus2
Name: bus, dtype: object