-
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: Added
Table.transform_table
method which returns the transfor…
…med Table (#229) Closes #110. ### Summary of Changes Added `Table.transform_table` Method which returns a Table transformed with the given `TableTransformer` Co-authored-by: Marsmaennchen221 <[email protected]> --------- Co-authored-by: megalinter-bot <[email protected]> Co-authored-by: Alexander Gréus <[email protected]> Co-authored-by: Lars Reimann <[email protected]>
- Loading branch information
1 parent
846bf23
commit 0a9ce72
Showing
3 changed files
with
157 additions
and
3 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
120 changes: 120 additions & 0 deletions
120
tests/safeds/data/tabular/containers/_table/test_transform_table.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,120 @@ | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.data.tabular.exceptions import TransformerNotFittedError, UnknownColumnNameError | ||
from safeds.data.tabular.transformation import OneHotEncoder | ||
|
||
|
||
class TestTransform: | ||
@pytest.mark.parametrize( | ||
("table", "column_names", "expected"), | ||
[ | ||
( | ||
Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "b", "c"], | ||
}, | ||
), | ||
None, | ||
Table.from_dict( | ||
{ | ||
"col1_a": [1.0, 0.0, 0.0, 0.0], | ||
"col1_b": [0.0, 1.0, 1.0, 0.0], | ||
"col1_c": [0.0, 0.0, 0.0, 1.0], | ||
}, | ||
), | ||
), | ||
( | ||
Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "b", "c"], | ||
"col2": ["a", "b", "b", "c"], | ||
}, | ||
), | ||
["col1"], | ||
Table.from_dict( | ||
{ | ||
"col1_a": [1.0, 0.0, 0.0, 0.0], | ||
"col1_b": [0.0, 1.0, 1.0, 0.0], | ||
"col1_c": [0.0, 0.0, 0.0, 1.0], | ||
"col2": ["a", "b", "b", "c"], | ||
}, | ||
), | ||
), | ||
( | ||
Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "b", "c"], | ||
"col2": ["a", "b", "b", "c"], | ||
}, | ||
), | ||
["col1", "col2"], | ||
Table.from_dict( | ||
{ | ||
"col1_a": [1.0, 0.0, 0.0, 0.0], | ||
"col1_b": [0.0, 1.0, 1.0, 0.0], | ||
"col1_c": [0.0, 0.0, 0.0, 1.0], | ||
"col2_a": [1.0, 0.0, 0.0, 0.0], | ||
"col2_b": [0.0, 1.0, 1.0, 0.0], | ||
"col2_c": [0.0, 0.0, 0.0, 1.0], | ||
}, | ||
), | ||
), | ||
], | ||
ids=["all columns", "one column", "multiple columns"], | ||
) | ||
def test_should_return_transformed_table( | ||
self, | ||
table: Table, | ||
column_names: list[str] | None, | ||
expected: Table, | ||
) -> None: | ||
transformer = OneHotEncoder().fit(table, column_names) | ||
assert table.transform_table(transformer) == expected | ||
|
||
def test_should_not_change_original_table(self) -> None: | ||
table = Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "c"], | ||
}, | ||
) | ||
|
||
transformer = OneHotEncoder().fit(table, None) | ||
table.transform_table(transformer) | ||
|
||
expected = Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "c"], | ||
}, | ||
) | ||
|
||
assert table == expected | ||
|
||
def test_should_raise_if_column_not_found(self) -> None: | ||
table_to_fit = Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "c"], | ||
}, | ||
) | ||
|
||
transformer = OneHotEncoder().fit(table_to_fit, None) | ||
|
||
table_to_transform = Table.from_dict( | ||
{ | ||
"col2": ["a", "b", "c"], | ||
}, | ||
) | ||
|
||
with pytest.raises(UnknownColumnNameError): | ||
table_to_transform.transform_table(transformer) | ||
|
||
def test_should_raise_if_not_fitted(self) -> None: | ||
table = Table.from_dict( | ||
{ | ||
"col1": ["a", "b", "c"], | ||
}, | ||
) | ||
|
||
transformer = OneHotEncoder() | ||
|
||
with pytest.raises(TransformerNotFittedError): | ||
table.transform_table(transformer) |