diff --git a/src/power_grid_model_io/functions/filters.py b/src/power_grid_model_io/functions/filters.py index e33d1dbe..fae303ed 100644 --- a/src/power_grid_model_io/functions/filters.py +++ b/src/power_grid_model_io/functions/filters.py @@ -18,10 +18,12 @@ def exclude_empty(row: pd.Series, col: str) -> bool: """ if col not in row: raise ValueError(f"The column: '{col}' cannot be found for the filter") - result = has_value(row[col]) - if isinstance(result, pd.Series): - return result.item() - return result + + col_value = row[col] + if isinstance(col_value, pd.Series): + col_value = col_value.item() + + return has_value(col_value) def exclude_value(row: pd.Series, col: str, value: float | str) -> bool: @@ -30,11 +32,12 @@ def exclude_value(row: pd.Series, col: str, value: float | str) -> bool: """ if col not in row: raise ValueError(f"The column: '{col}' cannot be found for the filter") - result = row[col] != value - # Sonar cloud false positive (S2583): result can be a pd.Series of bool - if isinstance(result, pd.Series): # NOSONAR - return result.item() - return result + + col_value = row[col] + if isinstance(col_value, pd.Series): + col_value = col_value.item() + + return col_value != value def exclude_all_columns_empty_or_zero(row: pd.Series, cols: List[str]) -> bool: diff --git a/tests/unit/functions/test_filters.py b/tests/unit/functions/test_filters.py index e32bd7e4..fb4a4fbd 100644 --- a/tests/unit/functions/test_filters.py +++ b/tests/unit/functions/test_filters.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: Contributors to the Power Grid Model project # # SPDX-License-Identifier: MPL-2.0 -from typing import Tuple +from typing import Any, Tuple from unittest.mock import MagicMock, patch import pandas as pd @@ -10,13 +10,21 @@ from power_grid_model_io.functions.filters import exclude_all_columns_empty_or_zero, exclude_empty, exclude_value +@pytest.mark.parametrize( + ("row_value", "expected_call"), + [ + (1, 1), + (pd.Series(1), 1), + ], +) @patch("power_grid_model_io.functions.filters.has_value") -def test_exclude_empty(mock_has_value: MagicMock): +def test_exclude_empty(mock_has_value: MagicMock, row_value: Any, expected_call: Any): col = "foo" - row = pd.Series({"foo": 1, "bar": "xyz"}) - actual = exclude_empty(row, col) - mock_has_value.assert_called_once_with(row[col]) - assert actual == mock_has_value.return_value + row = pd.Series([row_value, "xyz"], index=["foo", "bar"]) + actual = exclude_empty(row=row, col=col) + + mock_has_value.assert_called_once_with(expected_call) + assert actual is mock_has_value.return_value def test_exclude_empty__invalid_col(): @@ -31,9 +39,11 @@ def test_exclude_empty__invalid_col(): (4.0, "x", True), (3.0, 3.0, False), (3.2, 3.1, True), + (pd.Series([3]), 3, False), + (pd.Series([3]), 2, True), ], ) -def test_exclude_value(row_value: float, check_value: float, expected: bool): +def test_exclude_value(row_value: float | pd.Series, check_value: float, expected: bool): row = pd.Series({"foo": row_value}) actual = exclude_value(row=row, col="foo", value=check_value) assert actual == expected