-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Added Table.transform_table method which returns the transformed Table
#229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fec1f5f
Added Table.transform_table Method which returns the transformed Table
sibre28 cb4987d
style: apply automated linter fixes
megalinter-bot ae26b44
docs: Added example for `Table.transform_table()`
Marsmaennchen221 29f2af2
style: apply automated linter fixes
megalinter-bot d4b8156
Merge branch 'main' into 110-method-in-table-to-apply-transformer
lars-reimann 6ced056
test: order tests like source
lars-reimann 093d914
style: apply automated linter fixes
megalinter-bot ae16e2d
docs: add missing dots
lars-reimann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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: | ||
Marsmaennchen221 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @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) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.