From 7da0c70bbdb16f87489ca7a532a7ee72c2629880 Mon Sep 17 00:00:00 2001 From: Max Nutz Date: Wed, 25 Mar 2026 19:04:24 +0100 Subject: [PATCH 1/3] renew statistics function for 'Final Energy [by Sector]|Transportation' - structure is already given --- .../statistics_functions.py | 39 +++++++------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/pypsa_validation_processing/statistics_functions.py b/pypsa_validation_processing/statistics_functions.py index 007cfe3..2ab23cc 100644 --- a/pypsa_validation_processing/statistics_functions.py +++ b/pypsa_validation_processing/statistics_functions.py @@ -87,30 +87,21 @@ def Final_Energy_by_Sector__Transportation( Notes ----- - The actual extraction of transportation final energy from the network - collection will be implemented by the user. A typical call would be:: - - network_collection.statistics.energy_balance( - comps=["Load"], carrier="transport" - ) - - The current implementation returns a dummy value of ``0.0 MWh`` for the - year 2020 so that the end-to-end workflow can be tested. + Includes all transportation-relevant carriers for component Load. Vehicle to Grid + does not need to be evaluated, as evaluation is restricted to Load-Components only. """ # sum over all transportation-relevant sectors - 2 different units involved. - result = ( - n.statistics.energy_balance( - carrier=[ - "land transport EV", - "land transport fuel cell", - "kerosene for aviation", - "shipping methanol", - ], - components="Load", - groupby=["carrier", "unit", "country"], - direction="withdrawal", - ) - .groupby(["country", "unit"]) - .sum() + res = n.statistics.energy_balance( + carrier=[ + "land transport EV", + "land transport fuel cell", + "land transport oil", + "kerosene for aviation", + "shipping methanol", + "shipping oil", + ], + components="Load", + groupby=["carrier", "unit", "country"], + direction="withdrawal", ) - return result + return res From e85b60bf15ebbcfd97b91f1ebdd7928eb48cfe8c Mon Sep 17 00:00:00 2001 From: Max Nutz Date: Wed, 25 Mar 2026 19:13:10 +0100 Subject: [PATCH 2/3] forgot groupby-statement --- .../statistics_functions.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/pypsa_validation_processing/statistics_functions.py b/pypsa_validation_processing/statistics_functions.py index 2ab23cc..70fc6cf 100644 --- a/pypsa_validation_processing/statistics_functions.py +++ b/pypsa_validation_processing/statistics_functions.py @@ -91,17 +91,21 @@ def Final_Energy_by_Sector__Transportation( does not need to be evaluated, as evaluation is restricted to Load-Components only. """ # sum over all transportation-relevant sectors - 2 different units involved. - res = n.statistics.energy_balance( - carrier=[ - "land transport EV", - "land transport fuel cell", - "land transport oil", - "kerosene for aviation", - "shipping methanol", - "shipping oil", - ], - components="Load", - groupby=["carrier", "unit", "country"], - direction="withdrawal", + res = ( + n.statistics.energy_balance( + carrier=[ + "land transport EV", + "land transport fuel cell", + "land transport oil", + "kerosene for aviation", + "shipping methanol", + "shipping oil", + ], + components="Load", + groupby=["carrier", "unit", "country"], + direction="withdrawal", + ) + .groupby(["country", "unit"]) + .sum() ) return res From 5633169fbce9fc94467cb4c394d2919ff83c07b7 Mon Sep 17 00:00:00 2001 From: Max Nutz Date: Wed, 25 Mar 2026 19:18:56 +0100 Subject: [PATCH 3/3] adapt testing to test output-structure of pd.Series --- tests/test_statistics_functions.py | 36 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/test_statistics_functions.py b/tests/test_statistics_functions.py index bf16c45..89f5803 100644 --- a/tests/test_statistics_functions.py +++ b/tests/test_statistics_functions.py @@ -21,16 +21,16 @@ class TestFinalEnergyByCarrierElectricity: """Test suite for Final_Energy_by_Carrier__Electricity function.""" - def test_returns_dataframe(self, mock_network: MockPyPSANetwork): - """Test that the function returns a pandas DataFrame or Series.""" + def test_returns_series(self, mock_network: MockPyPSANetwork): + """Test that the function returns a pandas Series.""" result = Final_Energy_by_Carrier__Electricity(mock_network) - assert isinstance(result, (pd.DataFrame, pd.Series)) + assert isinstance(result, pd.Series) - def test_has_country_and_unit_index(self, mock_network: MockPyPSANetwork): - """Test that result has country and unit in the index.""" + def test_has_country_and_unit_multiindex(self, mock_network: MockPyPSANetwork): + """Test that result has MultiIndex with country and unit levels.""" result = Final_Energy_by_Carrier__Electricity(mock_network) - assert "country" in result.index.names - assert "unit" in result.index.names + assert isinstance(result.index, pd.MultiIndex) + assert result.index.names == ["country", "unit"] def test_not_empty(self, mock_network: MockPyPSANetwork): """Test that result is not empty.""" @@ -53,7 +53,9 @@ def test_multiple_networks(self, mock_network_collection: MockNetworkCollection) """Test processing multiple networks from collection.""" for network in mock_network_collection: result = Final_Energy_by_Carrier__Electricity(network) - assert isinstance(result, (pd.DataFrame, pd.Series)) + assert isinstance(result, pd.Series) + assert isinstance(result.index, pd.MultiIndex) + assert result.index.names == ["country", "unit"] assert len(result) > 0 @@ -65,16 +67,16 @@ def test_multiple_networks(self, mock_network_collection: MockNetworkCollection) class TestFinalEnergyBySectorTransportation: """Test suite for Final_Energy_by_Sector__Transportation function.""" - def test_returns_dataframe(self, mock_network: MockPyPSANetwork): - """Test that the function returns a pandas DataFrame or Series.""" + def test_returns_series(self, mock_network: MockPyPSANetwork): + """Test that the function returns a pandas Series.""" result = Final_Energy_by_Sector__Transportation(mock_network) - assert isinstance(result, (pd.DataFrame, pd.Series)) + assert isinstance(result, pd.Series) - def test_has_country_and_unit_index(self, mock_network: MockPyPSANetwork): - """Test that result has country and unit in the index.""" + def test_has_country_and_unit_multiindex(self, mock_network: MockPyPSANetwork): + """Test that result has MultiIndex with country and unit levels.""" result = Final_Energy_by_Sector__Transportation(mock_network) - assert "country" in result.index.names - assert "unit" in result.index.names + assert isinstance(result.index, pd.MultiIndex) + assert result.index.names == ["country", "unit"] def test_not_empty(self, mock_network: MockPyPSANetwork): """Test that result is not empty.""" @@ -97,5 +99,7 @@ def test_multiple_networks(self, mock_network_collection: MockNetworkCollection) """Test processing multiple networks from collection.""" for network in mock_network_collection: result = Final_Energy_by_Sector__Transportation(network) - assert isinstance(result, (pd.DataFrame, pd.Series)) + assert isinstance(result, pd.Series) + assert isinstance(result.index, pd.MultiIndex) + assert result.index.names == ["country", "unit"] assert len(result) > 0