-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform multiple columns of
Table
at once (#982)
### Summary of Changes * Rename `transform_column` to `transform_columns` * Rename parameter `name` to `selector` * `selector` can now be a list of column names * `transformer` can now optionally have a second parameter to receive the entire row
- Loading branch information
1 parent
38dc89c
commit 2db9069
Showing
8 changed files
with
186 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/safeds/_validation/_check_column_has_no_missing_values.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
tests/safeds/data/tabular/containers/_table/test_transform_column.py
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
tests/safeds/data/tabular/containers/_table/test_transform_columns.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from collections.abc import Callable | ||
|
||
import pytest | ||
|
||
from safeds.data.tabular.containers import Cell, Row, Table | ||
from safeds.exceptions import ColumnNotFoundError | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("table_factory", "selector", "transformer", "expected"), | ||
[ | ||
# no rows (constant value) | ||
( | ||
lambda: Table({"col1": []}), | ||
"col1", | ||
lambda _: Cell.from_literal(None), | ||
Table({"col1": []}), | ||
), | ||
# no rows (computed value) | ||
( | ||
lambda: Table({"col1": []}), | ||
"col1", | ||
lambda cell: 2 * cell, | ||
Table({"col1": []}), | ||
), | ||
# non-empty (constant value) | ||
( | ||
lambda: Table({"col1": [1, 2]}), | ||
"col1", | ||
lambda _: Cell.from_literal(None), | ||
Table({"col1": [None, None]}), | ||
), | ||
# non-empty (computed value) | ||
( | ||
lambda: Table({"col1": [1, 2]}), | ||
"col1", | ||
lambda cell: 2 * cell, | ||
Table({"col1": [2, 4]}), | ||
), | ||
# multiple columns transformed (constant value) | ||
( | ||
lambda: Table({"col1": [1, 2], "col2": [3, 4]}), | ||
["col1", "col2"], | ||
lambda _: Cell.from_literal(None), | ||
Table({"col1": [None, None], "col2": [None, None]}), | ||
), | ||
# multiple columns transformed (computed value) | ||
( | ||
lambda: Table({"col1": [1, 2], "col2": [3, 4]}), | ||
["col1", "col2"], | ||
lambda cell: 2 * cell, | ||
Table({"col1": [2, 4], "col2": [6, 8]}), | ||
), | ||
# lambda takes row parameter | ||
( | ||
lambda: Table({"col1": [1, 2], "col2": [3, 4]}), | ||
"col1", | ||
lambda cell, row: 2 * cell + row["col2"], | ||
Table({"col1": [5, 8], "col2": [3, 4]}), | ||
), | ||
], | ||
ids=[ | ||
"no rows (constant value)", | ||
"no rows (computed value)", | ||
"non-empty (constant value)", | ||
"non-empty (computed value)", | ||
"multiple columns transformed (constant value)", | ||
"multiple columns transformed (computed value)", | ||
"lambda takes row parameter", | ||
], | ||
) | ||
class TestHappyPath: | ||
def test_should_transform_columns( | ||
self, | ||
table_factory: Callable[[], Table], | ||
selector: str, | ||
transformer: Callable[[Cell], Cell] | Callable[[Cell, Row], Cell], | ||
expected: Table, | ||
) -> None: | ||
actual = table_factory().transform_columns(selector, transformer) | ||
assert actual == expected | ||
|
||
def test_should_not_mutate_receiver( | ||
self, | ||
table_factory: Callable[[], Table], | ||
selector: str, | ||
transformer: Callable[[Cell], Cell] | Callable[[Cell, Row], Cell], | ||
expected: Table, # noqa: ARG002 | ||
) -> None: | ||
original = table_factory() | ||
original.transform_columns(selector, transformer) | ||
assert original == table_factory() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("table", "selector"), | ||
[ | ||
(Table({"col1": [1, 2]}), "col2"), | ||
(Table({"col1": [1, 2]}), ["col1", "col2"]), | ||
], | ||
ids=[ | ||
"one column name", | ||
"multiple column names", | ||
], | ||
) | ||
def test_should_raise_if_column_not_found( | ||
table: Table, | ||
selector: str, | ||
) -> None: | ||
with pytest.raises(ColumnNotFoundError): | ||
table.transform_columns(selector, lambda cell: cell * 2) |