Skip to content

Commit efa7371

Browse files
committed
fix(exclude_empty): correcty handly pd.Series row[col] values
Signed-off-by: Vincent Koppen <[email protected]>
1 parent 7b5d1aa commit efa7371

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/power_grid_model_io/functions/filters.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ def exclude_empty(row: pd.Series, col: str) -> bool:
1818
"""
1919
if col not in row:
2020
raise ValueError(f"The column: '{col}' cannot be found for the filter")
21-
result = has_value(row[col])
22-
if isinstance(result, pd.Series):
23-
return result.item()
24-
return result
21+
22+
col_value = row[col]
23+
if isinstance(col_value, pd.Series):
24+
col_value = col_value.item()
25+
26+
return has_value(col_value)
2527

2628

2729
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:
3032
"""
3133
if col not in row:
3234
raise ValueError(f"The column: '{col}' cannot be found for the filter")
33-
result = row[col] != value
34-
# Sonar cloud false positive (S2583): result can be a pd.Series of bool
35-
if isinstance(result, pd.Series): # NOSONAR
36-
return result.item()
37-
return result
35+
36+
col_value = row[col]
37+
if isinstance(col_value, pd.Series):
38+
col_value = col_value.item()
39+
40+
return col_value != value
3841

3942

4043
def exclude_all_columns_empty_or_zero(row: pd.Series, cols: List[str]) -> bool:

tests/unit/functions/test_filters.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
22
#
33
# SPDX-License-Identifier: MPL-2.0
4-
from typing import Tuple
4+
from typing import Any, Tuple
55
from unittest.mock import MagicMock, patch
66

77
import pandas as pd
@@ -10,13 +10,21 @@
1010
from power_grid_model_io.functions.filters import exclude_all_columns_empty_or_zero, exclude_empty, exclude_value
1111

1212

13+
@pytest.mark.parametrize(
14+
("row_value", "expected_call"),
15+
[
16+
(1, 1),
17+
(pd.Series(1), 1),
18+
],
19+
)
1320
@patch("power_grid_model_io.functions.filters.has_value")
14-
def test_exclude_empty(mock_has_value: MagicMock):
21+
def test_exclude_empty(mock_has_value: MagicMock, row_value: Any, expected_call: Any):
1522
col = "foo"
16-
row = pd.Series({"foo": 1, "bar": "xyz"})
17-
actual = exclude_empty(row, col)
18-
mock_has_value.assert_called_once_with(row[col])
19-
assert actual == mock_has_value.return_value
23+
row = pd.Series([row_value, "xyz"], index=["foo", "bar"])
24+
actual = exclude_empty(row=row, col=col)
25+
26+
mock_has_value.assert_called_once_with(expected_call)
27+
assert actual is mock_has_value.return_value
2028

2129

2230
def test_exclude_empty__invalid_col():
@@ -31,9 +39,11 @@ def test_exclude_empty__invalid_col():
3139
(4.0, "x", True),
3240
(3.0, 3.0, False),
3341
(3.2, 3.1, True),
42+
(pd.Series([3]), 3, False),
43+
(pd.Series([3]), 2, True),
3444
],
3545
)
36-
def test_exclude_value(row_value: float, check_value: float, expected: bool):
46+
def test_exclude_value(row_value: float | pd.Series, check_value: float, expected: bool):
3747
row = pd.Series({"foo": row_value})
3848
actual = exclude_value(row=row, col="foo", value=check_value)
3949
assert actual == expected

0 commit comments

Comments
 (0)