Skip to content
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

feat: Added Table.transform_table method which returns the transformed Table #229

Merged
merged 8 commits into from
Apr 21, 2023
23 changes: 23 additions & 0 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
if TYPE_CHECKING:
from collections.abc import Callable, Iterable

from safeds.data.tabular.transformation import TableTransformer

from ._tagged_table import TaggedTable


Expand Down Expand Up @@ -991,6 +993,27 @@ def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Tabl
return self.replace_column(name, result)
raise UnknownColumnNameError([name])

def transform_table(self, transformer: TableTransformer) -> Table:
"""
Apply a learned transformation onto this table.

Parameters
----------
transformer : TableTransformer
The transformer which transforms the given table

Returns
-------
transformed_table : Table
The transformed table.

Raises
------
TransformerNotFittedError
If the transformer has not been fitted yet.
"""
Marsmaennchen221 marked this conversation as resolved.
Show resolved Hide resolved
return transformer.transform(self)

# ------------------------------------------------------------------------------------------------------------------
# Plotting
# ------------------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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
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)