From fafef15b19c604dacf2665b326db79ee194870b7 Mon Sep 17 00:00:00 2001 From: MaGering Date: Thu, 6 Jul 2023 16:15:59 +0200 Subject: [PATCH 01/16] Give only warning while unstacking if source and column not empty --- oemof_b3/tools/data_processing.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/oemof_b3/tools/data_processing.py b/oemof_b3/tools/data_processing.py index 44e6b5f8..8ca21de0 100644 --- a/oemof_b3/tools/data_processing.py +++ b/oemof_b3/tools/data_processing.py @@ -1053,9 +1053,10 @@ def unstack_timeseries(df): lost_columns = ["source", "comment"] for col in lost_columns: if col in list(df.columns): - logger.warning( - f"Caution any remarks in column '{col}' are lost after unstacking." - ) + if not _df[col].isna().any() or _df[col].values.any() == "None": + logger.warning( + f"Caution any remarks in column '{col}' are lost after unstacking." + ) # Process values of series values_series = [] From 8742b9ad82e9f55617eff483c8c1df2d29832092 Mon Sep 17 00:00:00 2001 From: MaGering Date: Thu, 6 Jul 2023 16:18:14 +0200 Subject: [PATCH 02/16] Add test file for unstacking warning if cols source and comment not empty --- ..._resources_timeseries_stacked_comments.csv | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/_files/oemof_b3_resources_timeseries_stacked_comments.csv diff --git a/tests/_files/oemof_b3_resources_timeseries_stacked_comments.csv b/tests/_files/oemof_b3_resources_timeseries_stacked_comments.csv new file mode 100644 index 00000000..d3b523d8 --- /dev/null +++ b/tests/_files/oemof_b3_resources_timeseries_stacked_comments.csv @@ -0,0 +1,19 @@ +id_ts;region;var_name;timeindex_start;timeindex_stop;timeindex_resolution;series;var_unit;source;comment +0;BB;biomass-st_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +1;BB;ch4-gt_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +2;BB;electricity-curtailment_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +3;BB;electricity-demand_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1889794.8071409904, 1870448.7801216468, 1803840.2010708163];;;We have a comment here. +4;BB;electricity-liion-battery_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +5;BB;electricity-liion-battery_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[882505.2, 1194379.0, 1289504.5];;;We have a comment here. +6;BB;electricity-shortage_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +7;BB;solar-pv_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +8;BB;wind-onshore_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1145114.0, 801141.1, 595497.8];;;We have a comment here. +9;BE;biomass-st_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +10;BE;ch4-gt_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +11;BE;electricity-curtailment_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +12;BE;electricity-demand_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1889794.8071409904, 1870448.7801216468, 1803840.2010708163];;;We have a comment here. +13;BE;electricity-liion-battery_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +14;BE;electricity-liion-battery_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[882505.2, 1194379.0, 1289504.5];;;We have a comment here. +15;BE;electricity-shortage_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +16;BE;solar-pv_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here. +17;BE;wind-onshore_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1145114.0, 801141.1, 595497.8];;;We have a comment here. From 1a61b671933d607d15b15186def6da42750acdab Mon Sep 17 00:00:00 2001 From: MaGering Date: Thu, 6 Jul 2023 16:18:44 +0200 Subject: [PATCH 03/16] Add draft of test for unstacking warning if cols source and comment not empty --- tests/test_data_processing.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 215eb371..50ab7036 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -38,6 +38,7 @@ def full_path(filename): ) path_file_sc_mixed_types = full_path("oemof_b3_resources_scalars_mixed_types.csv") path_file_ts_stacked = full_path("oemof_b3_resources_timeseries_stacked.csv") +path_file_ts_stacked_comments = full_path("oemof_b3_resources_timeseries_stacked_comments.csv") path_oemof_results_flows = full_path("oemof_results_flows.csv") path_oemof_b3_results_timeseries_flows = full_path( "oemof_b3_results_timeseries_flows.csv" @@ -482,6 +483,21 @@ def test_unstack_stack_scalars_on_example_data(): assert pd.testing.assert_frame_equal(df, df_stacked) is None +def test_unstack_warning_source_comment(capfd): + df_wo_comments = load_b3_timeseries(path_file_ts_stacked) + df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) + + # TODO: Assert that unstack_timeseries(df_wo_comments) does not give the warning: + # "Caution any remarks in column 'comments' are lost after unstacking." + # TODO: When the following is executed: + unstack_timeseries(df_wo_comments) + + # TODO: Assert WARNING with unstack_timeseries(df_with_comments): + # "WARNING - Caution any remarks in column 'comments' are lost after unstacking." + # TODO: When the following is executed: + unstack_timeseries(df_with_comments) + + def test_merge_a_into_b(): r""" Tests merge function. From 39ea4d2db3331224b8098f3cc10df9e132703549 Mon Sep 17 00:00:00 2001 From: MaGering Date: Thu, 6 Jul 2023 16:24:37 +0200 Subject: [PATCH 04/16] Apply black --- tests/test_data_processing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 50ab7036..7baab528 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -38,7 +38,9 @@ def full_path(filename): ) path_file_sc_mixed_types = full_path("oemof_b3_resources_scalars_mixed_types.csv") path_file_ts_stacked = full_path("oemof_b3_resources_timeseries_stacked.csv") -path_file_ts_stacked_comments = full_path("oemof_b3_resources_timeseries_stacked_comments.csv") +path_file_ts_stacked_comments = full_path( + "oemof_b3_resources_timeseries_stacked_comments.csv" +) path_oemof_results_flows = full_path("oemof_results_flows.csv") path_oemof_b3_results_timeseries_flows = full_path( "oemof_b3_results_timeseries_flows.csv" From e606a9375816eb17ac7360747e38e761f4f3238a Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Mon, 10 Jul 2023 17:22:26 +0200 Subject: [PATCH 05/16] Delete capfd in test_unstack_warning_source_comment --- tests/test_data_processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 7baab528..e828b974 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -485,7 +485,7 @@ def test_unstack_stack_scalars_on_example_data(): assert pd.testing.assert_frame_equal(df, df_stacked) is None -def test_unstack_warning_source_comment(capfd): +def test_unstack_warning_source_comment(): df_wo_comments = load_b3_timeseries(path_file_ts_stacked) df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) From d463fd39cfeddfeb91f5ad1fd76d80c2b6f15b5e Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Mon, 10 Jul 2023 17:46:52 +0200 Subject: [PATCH 06/16] Add docstring to test --- tests/test_data_processing.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index e828b974..21b549b0 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -486,6 +486,11 @@ def test_unstack_stack_scalars_on_example_data(): def test_unstack_warning_source_comment(): + """ + This test checks whether oemof-B3 scalars columns source and comments + are empty before unstacking. A caution message will be raised if source and comments are + not empty. + """ df_wo_comments = load_b3_timeseries(path_file_ts_stacked) df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) @@ -499,6 +504,7 @@ def test_unstack_warning_source_comment(): # TODO: When the following is executed: unstack_timeseries(df_with_comments) +test_unstack_warning_source_comment() def test_merge_a_into_b(): r""" From 840c85e58044a8cfd4275e92ea3b57e26a6a4cbd Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Mon, 10 Jul 2023 18:17:27 +0200 Subject: [PATCH 07/16] Add docstring to test --- tests/test_data_processing.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 21b549b0..8c34b87d 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -504,8 +504,6 @@ def test_unstack_warning_source_comment(): # TODO: When the following is executed: unstack_timeseries(df_with_comments) -test_unstack_warning_source_comment() - def test_merge_a_into_b(): r""" Tests merge function. From 279968b30846bcb38aaff2161b377f32894f1880 Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Mon, 17 Jul 2023 16:29:32 +0200 Subject: [PATCH 08/16] Apply black and update docstring --- tests/test_data_processing.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 8c34b87d..907f11dd 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -2,6 +2,8 @@ import numpy as np import pandas as pd import pytest +import unittest +import warnings from oemof_b3.tools.data_processing import ( HEADER_B3_SCAL, @@ -487,9 +489,9 @@ def test_unstack_stack_scalars_on_example_data(): def test_unstack_warning_source_comment(): """ - This test checks whether oemof-B3 scalars columns source and comments - are empty before unstacking. A caution message will be raised if source and comments are - not empty. + This test verifies whether the caution message is appropriately raised + when executing the function unstack_timeseries(). The caution message will + be raised if source and comments are not empty. """ df_wo_comments = load_b3_timeseries(path_file_ts_stacked) df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) @@ -504,6 +506,7 @@ def test_unstack_warning_source_comment(): # TODO: When the following is executed: unstack_timeseries(df_with_comments) + def test_merge_a_into_b(): r""" Tests merge function. From f55c41fafdb6982d0d80b52562f0eef622e318ff Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Mon, 17 Jul 2023 16:34:30 +0200 Subject: [PATCH 09/16] Remove unused libraries --- tests/test_data_processing.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 907f11dd..b36e5373 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -2,8 +2,6 @@ import numpy as np import pandas as pd import pytest -import unittest -import warnings from oemof_b3.tools.data_processing import ( HEADER_B3_SCAL, From 64af9d5d4a3be1252a76fe2b600f140dcc787beb Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Thu, 20 Jul 2023 17:51:51 +0200 Subject: [PATCH 10/16] Implement test for --- tests/test_data_processing.py | 48 +++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index b36e5373..40b53141 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -2,6 +2,8 @@ import numpy as np import pandas as pd import pytest +import unittest +from unittest.mock import patch from oemof_b3.tools.data_processing import ( HEADER_B3_SCAL, @@ -485,24 +487,48 @@ def test_unstack_stack_scalars_on_example_data(): assert pd.testing.assert_frame_equal(df, df_stacked) is None -def test_unstack_warning_source_comment(): +class test_unstack_warning_source_comment(unittest.TestCase): """ This test verifies whether the caution message is appropriately raised when executing the function unstack_timeseries(). The caution message will be raised if source and comments are not empty. """ - df_wo_comments = load_b3_timeseries(path_file_ts_stacked) - df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) - # TODO: Assert that unstack_timeseries(df_wo_comments) does not give the warning: - # "Caution any remarks in column 'comments' are lost after unstacking." - # TODO: When the following is executed: - unstack_timeseries(df_wo_comments) + def setUp(self): + self.df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) + self.df_wo_comments = load_b3_timeseries(path_file_ts_stacked) - # TODO: Assert WARNING with unstack_timeseries(df_with_comments): - # "WARNING - Caution any remarks in column 'comments' are lost after unstacking." - # TODO: When the following is executed: - unstack_timeseries(df_with_comments) + # Assert WARNING with unstack_timeseries(df_with_comments) + def test_unstack_warning_source_comment(self): + # Patch the logger.warning method to capture the warning calls + with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: + # Call the function with df_with_comments, which should raise a warning + unstacked_df = unstack_timeseries(self.df_with_comments) + + # Check if the logger.warning was called with the expected messages + expected_warnings = [ + "Caution any remarks in column 'source' are lost after unstacking.", + "Caution any remarks in column 'comment' are lost after unstacking." + ] + + # Check if any of the expected warning messages are contained in the captured warning messages + self.assertTrue(any( + warning_msg in str(warning[0]) for warning in mock_logger_warning.call_args_list for warning_msg in + expected_warnings)) + + # Assert that unstack_timeseries(df_wo_comments) does not give the warning: + def test_unstack_no_warning(self): + # Patch the logger.warning method to capture the warning calls + with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: + # Call the function with df_wo_comments, which should not raise a warning + unstacked_df = unstack_timeseries(self.df_wo_comments) + + # Check that logger.warning was not called in this case + mock_logger_warning.assert_not_called() + + +if __name__ == '__main__': + unittest.main() def test_merge_a_into_b(): From 470c8a1276777406cb8e7cff19346e16fbd2499b Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Thu, 20 Jul 2023 17:55:40 +0200 Subject: [PATCH 11/16] Implment test for function unstack_timeseries() --- tests/test_data_processing.py | 48 ++++++++--------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 40b53141..b36e5373 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -2,8 +2,6 @@ import numpy as np import pandas as pd import pytest -import unittest -from unittest.mock import patch from oemof_b3.tools.data_processing import ( HEADER_B3_SCAL, @@ -487,48 +485,24 @@ def test_unstack_stack_scalars_on_example_data(): assert pd.testing.assert_frame_equal(df, df_stacked) is None -class test_unstack_warning_source_comment(unittest.TestCase): +def test_unstack_warning_source_comment(): """ This test verifies whether the caution message is appropriately raised when executing the function unstack_timeseries(). The caution message will be raised if source and comments are not empty. """ + df_wo_comments = load_b3_timeseries(path_file_ts_stacked) + df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) - def setUp(self): - self.df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) - self.df_wo_comments = load_b3_timeseries(path_file_ts_stacked) + # TODO: Assert that unstack_timeseries(df_wo_comments) does not give the warning: + # "Caution any remarks in column 'comments' are lost after unstacking." + # TODO: When the following is executed: + unstack_timeseries(df_wo_comments) - # Assert WARNING with unstack_timeseries(df_with_comments) - def test_unstack_warning_source_comment(self): - # Patch the logger.warning method to capture the warning calls - with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: - # Call the function with df_with_comments, which should raise a warning - unstacked_df = unstack_timeseries(self.df_with_comments) - - # Check if the logger.warning was called with the expected messages - expected_warnings = [ - "Caution any remarks in column 'source' are lost after unstacking.", - "Caution any remarks in column 'comment' are lost after unstacking." - ] - - # Check if any of the expected warning messages are contained in the captured warning messages - self.assertTrue(any( - warning_msg in str(warning[0]) for warning in mock_logger_warning.call_args_list for warning_msg in - expected_warnings)) - - # Assert that unstack_timeseries(df_wo_comments) does not give the warning: - def test_unstack_no_warning(self): - # Patch the logger.warning method to capture the warning calls - with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: - # Call the function with df_wo_comments, which should not raise a warning - unstacked_df = unstack_timeseries(self.df_wo_comments) - - # Check that logger.warning was not called in this case - mock_logger_warning.assert_not_called() - - -if __name__ == '__main__': - unittest.main() + # TODO: Assert WARNING with unstack_timeseries(df_with_comments): + # "WARNING - Caution any remarks in column 'comments' are lost after unstacking." + # TODO: When the following is executed: + unstack_timeseries(df_with_comments) def test_merge_a_into_b(): From 99fc9fd93ae0bd35c03ec7a12e5b4efedc8f41fd Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Thu, 20 Jul 2023 18:01:48 +0200 Subject: [PATCH 12/16] Revert "Implment test for function unstack_timeseries()" This reverts commit 470c8a1276777406cb8e7cff19346e16fbd2499b. --- tests/test_data_processing.py | 48 +++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index b36e5373..40b53141 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -2,6 +2,8 @@ import numpy as np import pandas as pd import pytest +import unittest +from unittest.mock import patch from oemof_b3.tools.data_processing import ( HEADER_B3_SCAL, @@ -485,24 +487,48 @@ def test_unstack_stack_scalars_on_example_data(): assert pd.testing.assert_frame_equal(df, df_stacked) is None -def test_unstack_warning_source_comment(): +class test_unstack_warning_source_comment(unittest.TestCase): """ This test verifies whether the caution message is appropriately raised when executing the function unstack_timeseries(). The caution message will be raised if source and comments are not empty. """ - df_wo_comments = load_b3_timeseries(path_file_ts_stacked) - df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) - # TODO: Assert that unstack_timeseries(df_wo_comments) does not give the warning: - # "Caution any remarks in column 'comments' are lost after unstacking." - # TODO: When the following is executed: - unstack_timeseries(df_wo_comments) + def setUp(self): + self.df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments) + self.df_wo_comments = load_b3_timeseries(path_file_ts_stacked) - # TODO: Assert WARNING with unstack_timeseries(df_with_comments): - # "WARNING - Caution any remarks in column 'comments' are lost after unstacking." - # TODO: When the following is executed: - unstack_timeseries(df_with_comments) + # Assert WARNING with unstack_timeseries(df_with_comments) + def test_unstack_warning_source_comment(self): + # Patch the logger.warning method to capture the warning calls + with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: + # Call the function with df_with_comments, which should raise a warning + unstacked_df = unstack_timeseries(self.df_with_comments) + + # Check if the logger.warning was called with the expected messages + expected_warnings = [ + "Caution any remarks in column 'source' are lost after unstacking.", + "Caution any remarks in column 'comment' are lost after unstacking." + ] + + # Check if any of the expected warning messages are contained in the captured warning messages + self.assertTrue(any( + warning_msg in str(warning[0]) for warning in mock_logger_warning.call_args_list for warning_msg in + expected_warnings)) + + # Assert that unstack_timeseries(df_wo_comments) does not give the warning: + def test_unstack_no_warning(self): + # Patch the logger.warning method to capture the warning calls + with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: + # Call the function with df_wo_comments, which should not raise a warning + unstacked_df = unstack_timeseries(self.df_wo_comments) + + # Check that logger.warning was not called in this case + mock_logger_warning.assert_not_called() + + +if __name__ == '__main__': + unittest.main() def test_merge_a_into_b(): From 4dba4022fbc6f4abf1e8a6d881ececffdafb6d05 Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Thu, 20 Jul 2023 18:07:33 +0200 Subject: [PATCH 13/16] Apply black --- tests/test_data_processing.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 40b53141..76d94c08 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -501,25 +501,33 @@ def setUp(self): # Assert WARNING with unstack_timeseries(df_with_comments) def test_unstack_warning_source_comment(self): # Patch the logger.warning method to capture the warning calls - with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: + with patch( + "oemof_b3.tools.data_processing.logger.warning" + ) as mock_logger_warning: # Call the function with df_with_comments, which should raise a warning unstacked_df = unstack_timeseries(self.df_with_comments) # Check if the logger.warning was called with the expected messages expected_warnings = [ "Caution any remarks in column 'source' are lost after unstacking.", - "Caution any remarks in column 'comment' are lost after unstacking." + "Caution any remarks in column 'comment' are lost after unstacking.", ] # Check if any of the expected warning messages are contained in the captured warning messages - self.assertTrue(any( - warning_msg in str(warning[0]) for warning in mock_logger_warning.call_args_list for warning_msg in - expected_warnings)) + self.assertTrue( + any( + warning_msg in str(warning[0]) + for warning in mock_logger_warning.call_args_list + for warning_msg in expected_warnings + ) + ) # Assert that unstack_timeseries(df_wo_comments) does not give the warning: def test_unstack_no_warning(self): # Patch the logger.warning method to capture the warning calls - with patch('oemof_b3.tools.data_processing.logger.warning') as mock_logger_warning: + with patch( + "oemof_b3.tools.data_processing.logger.warning" + ) as mock_logger_warning: # Call the function with df_wo_comments, which should not raise a warning unstacked_df = unstack_timeseries(self.df_wo_comments) @@ -527,7 +535,7 @@ def test_unstack_no_warning(self): mock_logger_warning.assert_not_called() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() From 8bf2de464668b67707ff3d71b0e8850eda07695a Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Thu, 20 Jul 2023 18:13:58 +0200 Subject: [PATCH 14/16] Check build 3.8 --- tests/test_data_processing.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index 76d94c08..db5eb3a3 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -505,7 +505,7 @@ def test_unstack_warning_source_comment(self): "oemof_b3.tools.data_processing.logger.warning" ) as mock_logger_warning: # Call the function with df_with_comments, which should raise a warning - unstacked_df = unstack_timeseries(self.df_with_comments) + unstack_timeseries(self.df_with_comments) # Check if the logger.warning was called with the expected messages expected_warnings = [ @@ -513,7 +513,8 @@ def test_unstack_warning_source_comment(self): "Caution any remarks in column 'comment' are lost after unstacking.", ] - # Check if any of the expected warning messages are contained in the captured warning messages + # Check if any of the expected warning messages are contained + # in the captured warning messages self.assertTrue( any( warning_msg in str(warning[0]) @@ -529,7 +530,7 @@ def test_unstack_no_warning(self): "oemof_b3.tools.data_processing.logger.warning" ) as mock_logger_warning: # Call the function with df_wo_comments, which should not raise a warning - unstacked_df = unstack_timeseries(self.df_wo_comments) + unstack_timeseries(self.df_wo_comments) # Check that logger.warning was not called in this case mock_logger_warning.assert_not_called() From b06822748b8bee7e45dc54d423a4b11ca7cdd81e Mon Sep 17 00:00:00 2001 From: MaGering Date: Mon, 24 Jul 2023 11:54:21 +0200 Subject: [PATCH 15/16] Update test_unstack_warning_source_comment so that it is scanned for warnings due to entries in both columns 'source' and 'comment' --- tests/test_data_processing.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/test_data_processing.py b/tests/test_data_processing.py index db5eb3a3..64c9d4ad 100644 --- a/tests/test_data_processing.py +++ b/tests/test_data_processing.py @@ -515,12 +515,10 @@ def test_unstack_warning_source_comment(self): # Check if any of the expected warning messages are contained # in the captured warning messages + warnings_called = [call[0][0] for call in mock_logger_warning.call_args_list] + self.assertTrue( - any( - warning_msg in str(warning[0]) - for warning in mock_logger_warning.call_args_list - for warning_msg in expected_warnings - ) + any(warning_msg in warnings_called for warning_msg in expected_warnings) ) # Assert that unstack_timeseries(df_wo_comments) does not give the warning: From 37290dc238ac5d8bced649b728ac9a47cc960aba Mon Sep 17 00:00:00 2001 From: MaGering Date: Mon, 24 Jul 2023 11:56:23 +0200 Subject: [PATCH 16/16] Fix 'Give only warning while unstacking if source and column not empty' so that a warning is given if there is at least one value in columns 'source' and 'comment' --- oemof_b3/tools/data_processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oemof_b3/tools/data_processing.py b/oemof_b3/tools/data_processing.py index 8ca21de0..aea85d26 100644 --- a/oemof_b3/tools/data_processing.py +++ b/oemof_b3/tools/data_processing.py @@ -1053,7 +1053,7 @@ def unstack_timeseries(df): lost_columns = ["source", "comment"] for col in lost_columns: if col in list(df.columns): - if not _df[col].isna().any() or _df[col].values.any() == "None": + if not _df[col].isna().all() or _df[col].values.all() == "None": logger.warning( f"Caution any remarks in column '{col}' are lost after unstacking." )