Skip to content

Statistics Functions API

statistics_functions

Statistics functions for PyPSA validation processing.

Each function in this module corresponds to one IAMC variable and extracts the relevant value from a given PyPSA Network. The functions are looked up by name via the mapping defined in configs/mapping.default.yaml.

For statistics-functions, the fixed input is n = pypsa.Network (one network / investment year) and aggregate_per_year: bool = True to switch between yearly aggregation and full snapshot time series.

If a variable-specific function needs additional settings, optional function parameters can be added. Currently, these include config and energy totals. The functions signature is identified automatically.

Each function returns a :class:pandas.Series with MultiIndex, holding at least the indexes location and unit (for aggregate_per_year = True) or a :class:pandas.DataFrame with MultiIndex and snapshots as columns (for aggregate_per_year = False).

Region Level: Regions in the returned Series correspond to the network's bus regions (e.g., "AT1", "AT2", "AT3" for pypsa-at). The postprocessing layer in Network_Processor handles aggregation to country level or keeps regions based on the aggregation_level configuration by the name of the entries of location. All regions starting with the given country-entry are grouped together.

Final_Energy_by_Carrier__Electricity(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract Final Energy [by Carrier]|Electricity from a PyPSA Network.

Sum of the electricity withdrawn from low voltage buses by the sector consumers (agriculture, residential and commercial incl. decentral heat pumps and resistive heaters, BEV charger, industry electricity), minus the V2G feed-in to low voltage (per snapshot, not clipped), plus the DAC withdrawal from AC buses. Distribution grid, storage chargers and district-heat suppliers (urban central …) are excluded.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Notes

home battery charger (withdrawal) and home battery discharger (supply) are both excluded. Measured on the networks in resources/ (20261020_carbon_accounting_run, AT_KN2040, AT_KN2040_1H, electricity_network_0p3): the home battery stores are cyclic, so charger minus discharger equals the round-trip loss, charger · (1 − η²) with η = 0.98. The largest loss is 3.05 TWh (AT_KN2040_1H 2040, 0.06 % of the low voltage withdrawal); it is not counted.

Net_Imports__Electricity(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract net imports of electricity from a PyPSA Network.

For location l: NI(l) = Σ (−p_port) over all ports of AC Lines and DC Links at AC buses in l, i.e. the energy fed into l minus the energy withdrawn from it. Imports over a lossy branch are counted at the receiving port, exports at the sending port. Positive = net import, negative = net export. Country values follow from summing locations (C8, class flow).

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network; locations without branches get 0.0 (C6.13).

Notes

Limitation: the losses of branches between two locations of the same country are not cross-border flows, but stay in the country sum as negative net imports. The country value is therefore its cross-border net imports minus its internal branch losses (accepted bias, spec Net_Imports__Electricity.md section 7). Branches with both ends in the same location do not exist by model design.

Final_Energy_by_Carrier__Coal(n: pypsa.Network, aggregate_per_year: bool = True, industry_non_energy_shares: pd.DataFrame | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Carrier]|Coal from a PyPSA Network.

FE(l) = E(l) · (1 − κ) + E(l) · κ / 1.366 with E(l) = L[coal for industry](l) − NE_coal(l): the coal for industry Load minus its non-energy use (D5), with the coke correction (D6). Coal for power plants and CHPs is transformation, not final energy.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
industry_non_energy_shares DataFrame | None

Eurostat non-energy shares, index (country, carrier), column non_energy_share (C2.5). Without industry production data in the network, κ = 0 (Eurostat fallback, D6).

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Final_Energy_by_Carrier__Oil(n: pypsa.Network, aggregate_per_year: bool = True, energy_totals: pd.DataFrame | None = None, industry_non_energy_shares: pd.DataFrame | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Carrier]|Oil from a PyPSA Network.

FE(l) = f_oil · (L[agriculture machinery oil](l) + W[oil, decentral oil boilers](l) + L[land transport oil](l) + d_aviation(c) · s_ker(l) · L[kerosene for aviation](l) + d_navigation(c) · L[shipping oil](l) + L[naphtha for industry](l) − NE_naphtha(l)): the fossil share (D2) of the oil consumed by the sectors. Boilers are located per D1, domestic shares per country (D4), non-energy use per D5. s_ker is the yearly oil-based share of the kerosene supply at l. shipping methanol is not oil. Units are returned as pypsa reports them (D7).

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
energy_totals DataFrame | None

Energy totals of one year, indexed by country (C2.4), for the domestic shares of aviation and navigation (D4). Required if the network has a kerosene for aviation or shipping oil Load.

None
industry_non_energy_shares DataFrame | None

Eurostat non-energy shares, index (country, carrier), column non_energy_share (C2.5). Required if the network has a naphtha for industry Load.

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Raises:

Type Description
ValueError

If a carrier other than kerosene for aviation and methanol-to-kerosene supplies a kerosene for aviation bus; if energy_totals is None and the network has a kerosene for aviation or shipping oil Load; or if industry_non_energy_shares is None and the network has a naphtha for industry Load.

Net_Imports__Oil(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract net imports of oil from a PyPSA Network.

NI(l) = f_oil · (Σ_c W[oil, c](l) + ΔE_store(l)) − S[oil, oil refining](l): the fossil part of the oil withdrawn by Links (location per D1) and of the net withdrawal of oil Stores, minus the fossil production of the location (D2). Positive = net import. The copperplate EU, which holds the refining, is negative; the yearly sum over all locations is 0.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Notes

The result counts only oil the model consumes. Energy statistics (Eurostat IMP − EXP, Statistik Austria) count all net imports of oil, including oil for non-energy use and refinery own use. PyPSA-AT models neither, except naphtha for industry. So the result is lower than statistics by about non-energy use minus naphtha plus refinery own use. For AT 2024 (Eurostat), that is 65.6 − 13.7 + 23.1 ≈ 75 PJ, about 17 % of net imports. Take this into account in comparisons with statistics or other models.

Final_Energy_by_Carrier__Natural_Gas(n: pypsa.Network, aggregate_per_year: bool = True, industry_non_energy_shares: pd.DataFrame | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Carrier]|Natural Gas from a PyPSA Network.

FE(l) = f_gas · (W[gas, decentral gas boilers](l) + L[gas for industry](l) − NE_gas(l)): the fossil share (D3) of the gas withdrawn by decentral boilers (location per D1) and of the energy part of the gas for industry Load (non-energy use per D5). Central boilers, CHPs, power plants and SMR are transformation, not final energy.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
industry_non_energy_shares DataFrame | None

Eurostat non-energy shares, index (country, carrier), column non_energy_share (C2.5).

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Final_Energy_by_Carrier__District_Heat(n: pypsa.Network, aggregate_per_year: bool = True, config: dict | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Carrier]|District Heat from a PyPSA Network.

Heat delivered from district heating grids to consumers, without distribution losses: FE(l) = L[urban central heat](l) / (1 + λ) + L[low-temperature heat for industry](l) + W[DAC](l), all withdrawn from urban central heat buses at l. Storage chargers and the heat vent withdraw heat but are not consumers; supplying links are not counted.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
config dict | None

Network config; λ is config["sector"]["district_heating"]["district_heating_loss"].

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Raises:

Type Description
MissingInputError

If the district heating loss is missing in config.

ValueError

If a DAC Link supplies heat to an urban central heat bus, or a component with an unknown carrier withdraws from such a bus.

Final_Energy_by_Sector__Transportation(n: pypsa.Network, aggregate_per_year: bool = True, energy_totals: pd.DataFrame | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Sector]|Transportation from a PyPSA Network.

FE(l) = W[low voltage, BEV charger](l) − S[low voltage, V2G](l) + L[electricity for rail](l) + L[land transport fuel cell](l) + L[land transport oil](l) + d_aviation(c) · L[kerosene for aviation](l) + d_navigation(c) · (L[shipping oil](l) + L[shipping methanol](l)). The BEV charger withdrawal already includes charging losses and is not divided by an efficiency; the V2G feed-in is subtracted per snapshot, without clipping. Units are returned as pypsa reports them (incl. land transport, D7); Network_Processor normalises them.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
energy_totals DataFrame | None

Energy totals of one year, indexed by country (C2.4), for the domestic shares of aviation and navigation (D4).

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit and every location of the network (C6.13).

Final_Energy_by_Sector__Industry(n: pypsa.Network, aggregate_per_year: bool = True, industry_non_energy_shares: pd.DataFrame | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Sector]|Industry from a PyPSA Network.

Industry excluding non-energy use: industry electricity, low-temperature heat for industry on urban central heat buses, the industrial share of the decentral heat input (D8), solid biomass for industry and the gas, H2, naphtha and coal for industry Loads minus their non-energy use (D5); coal with the coke correction (D6). industry methanol and supply-link losses are not counted.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
industry_non_energy_shares DataFrame | None

Eurostat non-energy shares, index (country, carrier), column non_energy_share (C2.5). Required if the network has gas, naphtha or coal for industry Loads; Network_Processor skips the variable if the table is not available.

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit (MWh) and every location of the network (C6.13).

Raises:

Type Description
ValueError

If industry_non_energy_shares is None and the network has a gas, naphtha or coal for industry Load.

Notes

The non-energy use NE_x(l) (feedstocks) is determined with the Eurostat fallback (D5, C2.5), because the networks do not carry industry production data for the physical method of pypsa-de yet:

  • NE_x(l) = S[c, x] · L[x for industry](l), with S the share FC_IND_NE / (FC_IND_E + FC_IND_NE) of the Eurostat energy balance (FC_NE for coal) and c the country of location l (first two characters). x maps the Loads gas, naphtha and coal for industry to the Eurostat carriers Natural Gas, Oil and Coal.
  • A country without a share gets the mean share of all countries for that carrier, with a WARNING.
  • NE_H2 = 0 (Eurostat has no hydrogen share), with a WARNING.
  • The coke correction of coal uses κ = 0 (D6), with a WARNING.

See :func:~pypsa_validation_processing.utils.non_energy_use and :func:~pypsa_validation_processing.utils.apply_coke_correction.

Final_Energy_by_Sector__Agriculture(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract Final Energy [by Sector]|Agriculture from a PyPSA Network.

FE(l) = L[agriculture electricity](l) + L[agriculture machinery electric](l) + L[agriculture machinery oil](l) + D8_agr(l). Losses of the machinery oil supply Links are not final energy.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit (MWh) and every location of the network (C6.13).

Raises:

Type Description
ValueError

If an agriculture heat Load sits on an urban central heat bus.

Notes

Agricultural heat is produced decentrally on the rural heat bus, so it is not reported as heat in final energy. Instead, the final energy is the input carriers of the decentral heat supply (e.g. biomass, electricity of heat pumps, CHP heat output), allocated to agriculture by its share of the rural heat demand (D8, decentral_heat_split). The agriculture heat Load itself is therefore not counted.

Final_Energy_by_Sector__Residential_and_Commercial(n: pypsa.Network, aggregate_per_year: bool = True, config: dict | None = None) -> pd.Series | pd.DataFrame

Extract Final Energy [by Sector]|Residential and Commercial.

FE(l) = Σ W[low voltage, c](l) + L[urban central heat](l) / (1 + λ) + D8_rc(l). The electricity term covers the low voltage consumers of the sector; it excludes industry, agriculture, transport (rail, road, BEV charger), storage chargers, the distribution grid, district-heat suppliers and every Link supplying a decentral heat bus, whose input enters only via the residential/commercial share of the decentral heat input (D8).

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), aggregate over all snapshots and return a :class:pandas.Series. If False, return a :class:pandas.DataFrame with snapshots as columns.

True
config dict | None

Network config; λ is config["sector"]["district_heating"]["district_heating_loss"].

None

Returns:

Type Description
Series | DataFrame

Series (aggregate_per_year=True) or DataFrame (aggregate_per_year=False) with MultiIndex location, unit (MWh) and every location of the network (C6.13).

Raises:

Type Description
MissingInputError

If the district heating loss is missing in config.

Price__Final_Energy__Industry__Electricity(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract the price of electricity for industry from a PyPSA Network.

Marginal price at the bus of the industry electricity Load, weighted by its withdrawal, CO2 price included (D9; pypsa-de price_load). Aggregation class intensive (C6.12): the result holds a value and a weight row per location.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), yearly Load-weighted price and weighted withdrawal as :class:pandas.Series. If False, a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

MultiIndex location, unit (EUR_2020/MWh), quantity and every location of the network. Value NaN where the withdrawal is 0.0; value and weight NaN at locations without the Load (C6.13).

Price__Final_Energy__Industry__Gases(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract the price of gas for industry from a PyPSA Network.

Marginal price at the bus of the gas for industry Load, weighted by its withdrawal, CO2 price included (D9). Aggregation class intensive (C6.12): the result holds a value and a weight row per location.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), yearly Load-weighted price and weighted withdrawal as :class:pandas.Series. If False, a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

MultiIndex location, unit (EUR_2020/MWh), quantity and every location of the network. Value NaN where the withdrawal is 0.0; value and weight NaN at locations without the Load (C6.13).

Price__Final_Energy__Industry__Hydrogen(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract the price of H2 for industry from a PyPSA Network.

Marginal price at the bus of the H2 for industry Load, weighted by its withdrawal, CO2 price included (D9). Aggregation class intensive (C6.12): the result holds a value and a weight row per location.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), yearly Load-weighted price and weighted withdrawal as :class:pandas.Series. If False, a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

MultiIndex location, unit (EUR_2020/MWh), quantity and every location of the network. Value NaN where the withdrawal is 0.0; value and weight NaN at locations without the Load (C6.13).

Price__Final_Energy__Industry__Solids__Biomass(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract the price of solid biomass for industry from a PyPSA Network.

Marginal price at the bus of the solid biomass for industry Load, weighted by its withdrawal, CO2 price included (D9). Aggregation class intensive (C6.12): the result holds a value and a weight row per location.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), yearly Load-weighted price and weighted withdrawal as :class:pandas.Series. If False, a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

MultiIndex location, unit (EUR_2020/MWh), quantity and every location of the network. Value NaN where the withdrawal is 0.0; value and weight NaN at locations without the Load (C6.13).

Price__Final_Energy__Industry__Solids__Coal(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract the price of coal for industry from a PyPSA Network.

Marginal price at the bus of the coal for industry Load, weighted by its withdrawal, CO2 price included (D9). Aggregation class intensive (C6.12): the result holds a value and a weight row per location.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), yearly Load-weighted price and weighted withdrawal as :class:pandas.Series. If False, a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

MultiIndex location, unit (EUR_2020/MWh), quantity and every location of the network. Value NaN where the withdrawal is 0.0; value and weight NaN at locations without the Load (C6.13).

Price__Final_Energy__Industry__Liquids__Oil(n: pypsa.Network, aggregate_per_year: bool = True) -> pd.Series | pd.DataFrame

Extract the price of naphtha for industry from a PyPSA Network.

Marginal price at the bus of the naphtha for industry Load, weighted by its withdrawal, CO2 price included (D9). Aggregation class intensive (C6.12): the result holds a value and a weight row per location.

Parameters:

Name Type Description Default
n Network

PyPSA network to process.

required
aggregate_per_year bool

If True (default), yearly Load-weighted price and weighted withdrawal as :class:pandas.Series. If False, a :class:pandas.DataFrame with snapshots as columns.

True

Returns:

Type Description
Series | DataFrame

MultiIndex location, unit (EUR_2020/MWh), quantity and every location of the network. Value NaN where the withdrawal is 0.0; value and weight NaN at locations without the Load (C6.13).