Components
Components
¶
Components(ctype: ComponentType, n: Network | None = None, names: str | int | Sequence[int | str] | None = None, suffix: str = '')
Bases: ComponentsData, ComponentsDescriptorsMixin, ComponentsTransformMixin, ComponentsIndexMixin, ComponentsArrayMixin
flowchart TD
pypsa.Components[Components]
pypsa.components.components.ComponentsData[ComponentsData]
pypsa.components.descriptors.ComponentsDescriptorsMixin[ComponentsDescriptorsMixin]
pypsa.components.transform.ComponentsTransformMixin[ComponentsTransformMixin]
pypsa.components.index.ComponentsIndexMixin[ComponentsIndexMixin]
pypsa.components.array.ComponentsArrayMixin[ComponentsArrayMixin]
pypsa.components.abstract._ComponentsABC[_ComponentsABC]
pypsa.components.components.ComponentsData --> pypsa.Components
pypsa.components.descriptors.ComponentsDescriptorsMixin --> pypsa.Components
pypsa.components.abstract._ComponentsABC --> pypsa.components.descriptors.ComponentsDescriptorsMixin
pypsa.components.transform.ComponentsTransformMixin --> pypsa.Components
pypsa.components.index.ComponentsIndexMixin --> pypsa.Components
pypsa.components.abstract._ComponentsABC --> pypsa.components.index.ComponentsIndexMixin
pypsa.components.array.ComponentsArrayMixin --> pypsa.Components
pypsa.components.abstract._ComponentsABC --> pypsa.components.array.ComponentsArrayMixin
click pypsa.Components href "" "pypsa.Components"
click pypsa.components.components.ComponentsData href "" "pypsa.components.components.ComponentsData"
click pypsa.components.descriptors.ComponentsDescriptorsMixin href "" "pypsa.components.descriptors.ComponentsDescriptorsMixin"
click pypsa.components.transform.ComponentsTransformMixin href "" "pypsa.components.transform.ComponentsTransformMixin"
click pypsa.components.index.ComponentsIndexMixin href "" "pypsa.components.index.ComponentsIndexMixin"
click pypsa.components.array.ComponentsArrayMixin href "" "pypsa.components.array.ComponentsArrayMixin"
click pypsa.components.abstract._ComponentsABC href "" "pypsa.components.abstract._ComponentsABC"
Components base class.
Base class for container of energy system related assets, such as generators or transmission lines. Use the specific subclasses for concrete or a generic component type. All data is stored in the dataclass ComponentsData. Components inherits from it, adds logic and methods, but does not store any data itself.
Parameters:
-
ctype(ComponentType) –Component type information.
-
n(Network, default:None) –Network object to attach to, by default None.
-
names((str, int, Sequence[int | str]), default:None) –Names of components to attach to, by default None.
-
suffix(str, default:'') –Suffix to add to component names, by default "".
-
API Reference
- Components
- Networks Network
- User Guide
- Home Overview Release Notes
-
API Reference
Components
commonas_components
- API Reference Components Component Types
-
API Reference
-
Components
commonas_components -
Other
Consistency Checks
Specific checks-
check_cost_consistency -
check_dispatch_delays -
check_dtypes_ -
check_for_unknown_buses -
check_for_unknown_carriers -
check_for_zero_impedances -
check_for_zero_s_nom -
check_generators -
check_link_delays -
check_nans_for_component_default_attrs -
check_static_power_attributes -
check_time_series -
check_time_series_power_attributes
-
-
Networks
Network
NetworkTransformMixinrename_component_names
-
Components
Methods:
-
get_activity_mask–Get active components mask indexed by snapshots.
-
get_active_assets–Get active components mask of component type in investment period(s).
-
rename_component_names–Rename component names.
-
filter_by_active_assets–Filter DataFrame or Index to only include active assets.
-
__str__–Get string representation of component.
-
__repr__–Get representation of component.
-
__eq__–Check if two Components are equal.
-
__len__–Get the number of components.
-
equals–Check if two Components are equal.
-
get_committable_big_m_values–Get per-asset big-M values for committable+extendable constraints.
Attributes:
-
static(DataFrame) –Dataframe with static data for all components of this type.
-
dynamic(Dict) –Dataframe with dynamic data for all components of this type.
-
names(Series) –Get component names.
-
snapshots(Index | MultiIndex) –Snapshots of the network.
-
has_scenarios(bool) –Boolean indicating if the network has scenarios defined.
-
scenarios(Index) –Scenarios of networks.
-
has_investment_periods(bool) –Indicator whether network has investment periods.
-
has_periods(bool) –Investment periods of the network.
-
investment_periods(Index) –Investment periods of the network.
-
da–xArray accessor to get component attributes as xarray DataArray.
-
component_names(Series) –Get component names.
-
timesteps(Index) –Time steps of the network.
-
periods(Index) –Periods of the network.
-
active_assets(Series) –Get list of active assets.
-
inactive_assets(Series) –Get list of inactive assets.
-
standard_types(DataFrame | None) –Get standard types of component.
-
name(str) –Name of component type.
-
list_name(str) –List name of component type.
-
description(str) –Description of component.
-
category(str) –Category of component.
-
type(str) –Get category of component.
-
attrs(DataFrame) –Default values of corresponding component type.
-
defaults(DataFrame) –Default values of corresponding component type.
-
empty(bool) –Check if component is empty.
-
attached(bool) –Check if component is attached to a Network.
-
n_save(Any) –A save property to access the network (component must be attached).
-
df(DataFrame) –Get static data of all components as pandas DataFrame.
-
pnl(dict) –Get dynamic data of all components as a dictionary of pandas DataFrames.
-
ds(Dataset) –Create a xarray data array view of the component.
-
units(Series) –Get units of all attributes of components.
-
ports(list) –Get ports of all components.
-
unique_carriers(set[str]) –Get all unique carrier values for this component.
-
extendables(Index) –Get the index of extendable elements of this component.
-
fixed(Index) –Get the index of non-extendable elements of this component.
-
committables(Index) –Get the index of committable elements of this component.
-
modulars(Index) –Get the index of modular elements of this component.
-
periodized_cost(DataArray) –Calculate periodized cost from component attributes as xarray DataArray.
-
capital_cost(Series) –Calculate annuitized investment cost per unit of capacity (no fom).
-
nyears(float | Series) –Return the modeled time horizon in years.
-
annuity(Series) –Calculate annuity factor for all components.
-
overnight_cost(Series) –Calculate overnight cost from component attributes.
pypsa.Components.static
instance-attribute
¶
static: DataFrame
Dataframe with static data for all components of this type.
Returns:
-
DataFrame–Static data of the component.
Examples:
>>> c.static
pypsa.Components.dynamic
instance-attribute
¶
dynamic: Dict
Dataframe with dynamic data for all components of this type.
Returns:
-
DataFrame–Dynamic data of the component.
Examples:
>>> c.dynamic
pypsa.Components.names
property
¶
names: Series
Get component names.
The names are corresponding to c.static.index for
non stochastic networks. For stochastic networks the index will be,
multi-indexed while here only the name dimension is returned.
Returns:
-
Series–Series with component names as index and values.
Examples:
>>> n = pypsa.Network()
>>> n.add("Generator", "g1")
>>> n.add("Generator", "g2")
>>> n.generators.index
Index(['g1', 'g2'], dtype='object', name='name')
pypsa.Components.snapshots
property
¶
snapshots: Index | MultiIndex
pypsa.Components.has_scenarios
property
¶
has_scenarios: bool
Boolean indicating if the network has scenarios defined.
pypsa.Components.has_investment_periods
property
¶
has_investment_periods: bool
Indicator whether network has investment periods.
See Also
pypsa.Components.investment_periods
property
¶
investment_periods: Index
pypsa.Components.da
instance-attribute
¶
da = _XarrayAccessor(self)
xArray accessor to get component attributes as xarray DataArray.
Examples:
>>> c = n.components.generators
>>> c.da.p_max_pu
<xarray.DataArray 'p_max_pu' (snapshot: 10, name: 6)> Size: 480B
array([[0.93001988, 1. , 0.9745832 , 1. , 0.5590784 ,
...
1. ]])
Coordinates:
* snapshot (snapshot) datetime64[ns] 80B 2015-01-01 ... 2015-01-01T09:00:00
* name (name) object 48B 'Manchester Wind' ... 'Frankfurt Gas'
For stochastic networks the scenarios are unstacked automatically:
>>> c = n_stoch.components.generators
>>> c.da.p_max_pu
<xarray.DataArray 'p_max_pu' (snapshot: 2920, scenario: 3, name: 4)> Size: 280kB
array([[[0. , 0.1566, 1. , 1. ],
...
[0. , 0.1082, 1. , 1. ]]], shape=(2920, 3, 4))
Coordinates:
* snapshot (snapshot) datetime64[ns] 23kB 2015-01-01 ... 2015-12-31T21:00:00
* scenario (scenario) object 24B 'low' 'med' 'high'
* name (name) object 32B 'solar' 'wind' 'gas' 'lignite'
String representation:
>>> c.da
<XarrayAccessor for Generators>
pypsa.Components.component_names
property
¶
component_names: Series
pypsa.Components.timesteps
property
¶
timesteps: Index
pypsa.Components.periods
property
¶
periods: Index
pypsa.Components.active_assets
property
¶
active_assets: Series
Get list of active assets.
See corresponding pypsa.Components.inactive_assets for details.
Returns:
-
Series–List of inactive assets
See Also
pypsa.Components.inactive_assets
property
¶
inactive_assets: Series
Get list of inactive assets.
An asset is considered inactive when one of the following conditions is met:
- active is set to False across all dimensions (investment periods, scenarios)
- build_year + lifetime never satisfies the condition for any investment period
Inactive assets are not considered in the optimization and are excluded from the model entirely.
Returns:
-
Series–List of inactive assets
Examples:
>>> n = pypsa.Network()
>>> n.snapshots = pd.MultiIndex.from_product([[2020, 2021, 2022], ["1", "2", "3"]])
>>> n.add("Generator", "g1", build_year=2020, lifetime=1)
>>> n.add("Generator", "g2", active=False)
List all components
>>> n.generators.index
Index(['g1', 'g2'], dtype='object', name='name')
List of inactive components 'g1' will be considered as active because it is active in at least one investment period
>>> n.components.generators.inactive_assets
Index(['g2'], dtype='object', name='name')
List of active components
>>> n.components.generators.active_assets
Index(['g1'], dtype='object', name='name')
c.active_assets and c.inactive_assets are mutually exclusive
See Also
pypsa.Components.standard_types
property
¶
standard_types: DataFrame | None
Get standard types of component.
Returns:
-
DataFrame–DataFrame with standard types of component.
Examples:
>>> n.components.transformers.standard_types
pypsa.Components.name
property
¶
name: str
Name of component type.
Returns:
-
str–Name of component.
Examples:
>>> n.components.generators.name
'Generator'
pypsa.Components.list_name
property
¶
list_name: str
List name of component type.
Returns:
-
str–List name of component.
Examples:
>>> n.components.generators.list_name
'generators'
pypsa.Components.description
property
¶
description: str
Description of component.
Returns:
-
str–Description of component.
Examples:
>>> n.components.generators.description
'Power generator for the bus carrier it attaches to.'
pypsa.Components.category
property
¶
category: str
Category of component.
Returns:
-
str–Category of component.
Examples:
>>> n.components.generators.category
'controllable_one_port'
pypsa.Components.type
property
¶
type: str
pypsa.Components.attrs
property
¶
attrs: DataFrame
Default values of corresponding component type.
Deprecated in v1.0.0
Use c.defaults instead.
Returns:
-
DataFrame–DataFrame with component attribute names as index and the information like type, unit, default value and description as columns.
pypsa.Components.defaults
property
¶
defaults: DataFrame
Default values of corresponding component type.
Returns:
-
DataFrame–DataFrame with component attribute names as index and the information like type, unit, default value and description as columns.
Examples:
>>> n.components.generators.defaults.head()
type unit default description status static varying typ dtype
attribute
name string NaN Unique name Input (required) True False <class 'str'> object
bus string NaN name of bus to which generator is attached Input (required) True False <class 'str'> object
control string NaN PQ P,Q,V control strategy for PF, must be "PQ", "... Input (optional) True False <class 'str'> object
type string NaN Placeholder for generator type. Not yet implem... Input (optional) True False <class 'str'> object
p_nom float MW 0.0 Nominal power for limits in optimization. Input (optional) True False <class 'float'> float64
pypsa.Components.empty
property
¶
empty: bool
Check if component is empty.
Returns:
-
bool–True if component is empty, otherwise False.
Examples:
>>> n = pypsa.Network()
>>> n.add('Generator', 'g1')
>>> n.components.generators.empty
False
>>> n.components.buses.empty
True
pypsa.Components.attached
property
¶
attached: bool
Check if component is attached to a Network.
Some functionality of the component is only available when attached to a Network.
Returns:
-
bool–True if component is attached to a Network, otherwise False.
Examples:
>>> n.components.generators.attached
True
pypsa.Components.n_save
property
¶
n_save: Any
pypsa.Components.df
property
¶
df: DataFrame
pypsa.Components.pnl
property
¶
pnl: dict
pypsa.Components.ds
property
¶
ds: Dataset
Create a xarray data array view of the component.
Note
Note that this will create a full copy of the component data. For large networks this may be a bottleneck. Use the pypsa.Components.da accessor instead to access a specific attribute of the component.
Returns:
-
Dataset–Dataset with component attributes as variables and snapshots as coordinates.
See Also
Examples:
>>> c = n.components.generators
>>> c.ds
<xarray.Dataset> Size: ...
Dimensions: (name: 6, snapshot: 10)
Coordinates:
* name (name) object ... 'Manchester Wind' ... 'Frankfu...
* snapshot (snapshot) datetime64[ns] ... 2015-01-01 ... 201...
Data variables: (12/43)
bus (name) object ... 'Manchester' ... 'Frankfurt'
control (name) object ... 'Slack' 'PQ' ... 'Slack' 'PQ'
type (name) object ... '' '' '' '' '' ''
p_nom (name) float64 ... 80.0 5e+04 100.0 ... 110.0 8e+04
p_nom_mod (name) float64 ... 0.0 0.0 0.0 0.0 0.0 0.0
p_nom_extendable (name) bool ... True True True True True True
...
pypsa.Components.units
property
¶
units: Series
Get units of all attributes of components.
Returns:
-
Series–Series with attribute names as index and units as values.
Examples:
>>> c = n.components.generators
>>> c.units.head()
unit
attribute
p_nom MW
p_nom_mod MW
p_nom_min MW
p_nom_max MW
p_min_pu per unit
pypsa.Components.ports
property
¶
ports: list
Get ports of all components.
Returns:
-
Series–Series with attribute names as index and port names as values.
Examples:
>>> c = n.components.lines
>>> c.ports
['0', '1']
pypsa.Components.unique_carriers
property
¶
unique_carriers: set[str]
Get all unique carrier values for this component.
Returns:
-
set of str–Set of all unique carrier names found in this component.
Examples:
>>> sorted(n.c.generators.unique_carriers)
['gas', 'wind']
>>> sorted(n.c.buses.unique_carriers)
['AC', 'DC']
pypsa.Components.extendables
property
¶
extendables: Index
Get the index of extendable elements of this component.
Returns:
-
Index–Single-level index of extendable elements.
pypsa.Components.fixed
property
¶
fixed: Index
Get the index of non-extendable elements of this component.
Returns:
-
Index–Single-level index of non-extendable elements.
pypsa.Components.committables
property
¶
committables: Index
Get the index of committable elements of this component.
Returns:
-
Index–Single-level index of committable elements.
pypsa.Components.modulars
property
¶
modulars: Index
Get the index of modular elements of this component.
Modular components have a positive module size (e.g., p_nom_mod > 0) which introduces integer variables for capacity expansion.
Returns:
-
Index–Single-level index of modular elements.
pypsa.Components.periodized_cost
property
¶
periodized_cost: DataArray
Calculate periodized cost from component attributes as xarray DataArray.
See Also
pypsa.costs.periodized_cost
pypsa.Components.capital_cost
property
¶
capital_cost: Series
Calculate annuitized investment cost per unit of capacity (no fom).
See Also
pypsa.costs.periodized_cost
pypsa.Components.nyears
property
¶
nyears: float | Series
pypsa.Components.annuity
property
¶
annuity: Series
Calculate annuity factor for all components.
Returns the annuity factor based on discount_rate and lifetime.
If discount_rate is NaN (no overnight_cost provided), returns 1.0.
Returns:
-
Series–Annuity factor for each component.
Examples:
>>> n.c.generators.annuity
name
gen1 0.085...
gen2 1.0
dtype: float64
See Also
pypsa.costs.annuity_factor
pypsa.Components.overnight_cost
property
¶
overnight_cost: Series
Calculate overnight cost from component attributes.
If overnight_cost column is provided (not NaN), returns it directly. Otherwise, converts annualized capital_cost back to overnight cost using the formula: overnight_cost = capital_cost / (annuity_factor × nyears).
Note: When nyears == 1, capital_cost represents the annualized cost per year, so overnight_cost = capital_cost / annuity_factor.
Returns:
-
Series–Overnight (upfront) investment cost per unit of capacity.
Examples:
>>> n.c.generators.overnight_cost
name
gen1 1000.0 # overnight_cost used directly
gen2 1166.0 # 100 / annuity(0.07, 25) - back-calculated from capital_cost
dtype: float64
See Also
capital_cost : Annuitized investment cost for the modeled horizon.
annuity : Annuity factor for each component.
pypsa.Components.get_activity_mask
¶
get_activity_mask(sns: Sequence | None = None, index: Index | None = None) -> DataFrame
Get active components mask indexed by snapshots.
Gets the boolean mask for active components, indexed by snapshots and components instead of just components.
Parameters:
-
sns(Index, default:None) –Set of snapshots for the mask. If None (default) all snapshots are returned.
-
index(Index, default:None) –Subset of the component elements. If None (default) all components are returned.
Examples:
>>> n = pypsa.Network()
>>> n.snapshots = pd.MultiIndex.from_product([[2020, 2021, 2022], ["1", "2", "3"]])
>>> n.add("Generator", "g1", build_year=2020, lifetime=1)
>>> n.add("Generator", "g2", active=False)
>>> n.components.generators.get_activity_mask()
name g1 g2
period timestep
2020 1 True False
2 True False
3 True False
2021 1 False False
2 False False
3 False False
2022 1 False False
2 False False
3 False False
pypsa.Components.get_active_assets
¶
get_active_assets(investment_period: int | str | Sequence | None = None) -> Series
Get active components mask of component type in investment period(s).
A component is considered active when:
- it's active attribute is True
- it's build year + lifetime is smaller than the investment period (if given)
Parameters:
-
investment_period((int, str, Sequence), default:None) –Investment period(s) to check for active within build year and lifetime. If none only the active attribute is considered and build year and lifetime are ignored. If multiple periods are given the mask is True if component is active in any of the given periods.
Returns:
-
Series–Boolean mask for active components
Examples:
Without investment periods
>>> n = pypsa.Network()
>>> n.add("Generator", "g1", active=False)
>>> n.add("Generator", "g2", active=True)
>>> n.components.generators.get_active_assets()
name
g1 False
g2 True
Name: active, dtype: bool
With investment periods
>>> n = pypsa.Network()
>>> n.snapshots = pd.MultiIndex.from_product([[2020, 2021, 2022], ["1", "2", "3"]])
>>> n.add("Generator", "g1", build_year=2020, lifetime=1)
>>> n.add("Generator", "g2", active=False)
>>> n.components.generators.get_active_assets()
name
g1 True
g2 False
Name: active, dtype: bool
-
API Reference
Networks
Network
NetworkDescriptorsMixinget_active_assets
pypsa.Components.rename_component_names
¶
rename_component_names(**kwargs: str) -> None
Rename component names.
Rename components and also update all cross-references of the component in the network.
Parameters:
-
**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")
>>> c = n.c.buses
Now rename the bus
>>> c.rename_component_names(bus1="bus2")
Which updates the bus components
>>> c.static.index
Index(['bus2'], dtype='object', name='name')
and all references in the network
>>> n.generators.bus
name
gen1 bus2
Name: bus, dtype: object
pypsa.Components.filter_by_active_assets
¶
filter_by_active_assets(data: DataFrame | Index, investment_period: int | float | Sequence | None = None) -> DataFrame | Index
Filter DataFrame or Index to only include active assets.
Parameters:
-
data(DataFrame | Index) –DataFrame or Index to filter. Must have a "name" level in its index.
-
investment_period(int | float | Sequence, default:None) –If provided, additionally filter by assets active in this specific investment period(s). If a sequence is given, assets active in any of the periods are included. NaN values are treated as None (no period filtering).
Returns:
-
DataFrame | Index–Filtered DataFrame or Index with only active assets.
Examples:
>>> n = pypsa.Network()
>>> n.add("Bus", "bus")
>>> n.add("Generator", "g1", bus="bus")
>>> n.add("Generator", "g2", bus="bus", active=False)
>>> df = n.generators[['p_nom', 'bus']]
>>> n.components.generators.filter_by_active_assets(df)
p_nom bus
name
g1 0.0 bus
pypsa.Components.__str__
¶
__str__() -> str
Get string representation of component.
Examples:
>>> str(n.components.generators)
"'Generator' Components"
pypsa.Components.__repr__
¶
__repr__() -> str
Get representation of component.
Examples:
>>> c = n.components.generators
>>> c
'Generator' Components
----------------------
Attached to PyPSA Network 'AC-DC-Meshed'
Components: 6
pypsa.Components.__eq__
¶
__eq__(other: object) -> bool
Check if two Components are equal.
Does not check the attached Network, but only component specific data. Therefore two components can be equal even if they are attached to different networks.
Parameters:
-
other(Any) –Other object to compare with.
Returns:
-
bool–True if components are equal, otherwise False.
See Also
pypsa.Components.__len__
¶
__len__() -> int
Get the number of components.
Returns:
-
int–Number of components.
Examples:
>>> len(n.components.generators)
6
Which is the same as:
>>> n.components.generators.static.shape[0]
6
pypsa.Components.equals
¶
equals(other: Any, log_mode: str = 'silent') -> bool
Check if two Components are equal.
Does not check the attached Network, but only component specific data. Therefore two components can be equal even if they are attached to different networks.
Parameters:
-
other(Any) –The other network to compare with.
-
log_mode(str, default:'silent') –Controls how differences are reported: - 'silent': No logging, just returns True/False - 'verbose': Prints differences but doesn't raise errors - 'strict': Raises ValueError on first difference
Raises:
-
ValueError–If log_mode is 'strict' and components are not equal.
Returns:
-
bool–True if components are equal, otherwise False.
Examples:
>>> n1 = pypsa.Network()
>>> n2 = pypsa.Network()
>>> n1.add("Bus", "bus1")
>>> n2.add("Bus", "bus1")
>>> n1.buses.equals(n2.buses)
True
pypsa.Components.get_committable_big_m_values
¶
get_committable_big_m_values(names: Index, max_pu: DataArray | None = None, committable_big_m: float | None = None) -> DataArray
Get per-asset big-M values for committable+extendable constraints.
ComponentsData
dataclass
¶
ComponentsData(ctype: ComponentType, n: Network | None, static: DataFrame, dynamic: Dict)
Dataclass for Components.
This class is used to store all data of a Components object. Other classes inherit from this class to implement logic and methods, but do not store any data next to the data in here.
All attributes can therefore also be accessed directly from
any Components object (which defines all
attributes and properties which are available for all component types) as well as
in specific type classes as Generators (which
define logic and methods specific to the component type).
Attributes:
-
ctype(ComponentType) –Component type information containing all default values and attributes. #TODO
-
n(Network | None) –Network to which the component might be attached.
-
static(DataFrame) –Static data of components as a pandas DataFrame. Columns are the attributes and the index is the component name.
-
dynamic(dict) –Dynamic (time-varying) data of components as a dict-like object of pandas DataFrames. Keys of the dict are the attribute names and each value is a pandas DataFrame with snapshots as index and the component names as columns.
-
API Reference
Components
Components
Components
-
API Reference
Components
Components
Components
pypsa.components.components.ComponentsData.dynamic
instance-attribute
¶
dynamic: Dict
Dataframe with dynamic data for all components of this type.
Returns:
-
DataFrame–Dynamic data of the component.
Examples:
>>> c.dynamic
pypsa.components.components.ComponentsData.static
instance-attribute
¶
static: DataFrame
Dataframe with static data for all components of this type.
Returns:
-
DataFrame–Static data of the component.
Examples:
>>> c.static